Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this code:

var object1 = {same:'test'}
var object2 = {same:'test'};       
console.log(object1 === object2)

It returns false in the console.

I also have this code:

var object1 = {same:'test'}
var object2 = object1;
console.log(object1 === object2)  

It returns true in the console.

I know '===' is an equality operator but I don't know how it works on objects.

Why does the first example return false?

share|improve this question
    
Objects are always compared by reference, not equality –  Willem D'haeseleer 17 hours ago
    
Removed incorrect "duplicate" flag. –  Niet the Dark Absol 5 hours ago

9 Answers 9

See this ball? Its colour is red. Call it ball1.

See this ball? Its colour is red. Call it ball2.

Is ball1 the same object as ball2? No, they are distinct objects that happen to have identical properties.


See this ball? Its colour is red. Call it ball1.

Let's call ball1, ball2.

Is ball1 the same object as ball2? Yes. They are the same ball.

share|improve this answer
7  
Good analogy. Also consider what happens to ball1 if we decide to paint ball2 green. –  tobyink 9 hours ago

Objects are compared by reference equality, and since object1 and object2 are two distinct instances, they are different objects (think of it as: they occupy different places in memory).

If however you assign object1 to object2, they both point to the same place in memory and thus are equal (they are two references to the same single object, it only exists once in memory). Changing a property through object1.same = 'uiae'; will update the property object2.same as well (because they are in fact the same identical object and the same property)

share|improve this answer
var object1 ={same:'test'}
var object2 ={same:'test'};       
console.log(object1 === object2)

In this case you are creating two different objects. that's why it returns false value in console when checking with === operator.

var object1 ={same:'test'}
var object2 =object1;
console.log(object1 === object2)

In this case you are creating one object and object1 & object2 referencing to created object, that's why it returns true when you are checking with === operator.

In second case if we change the object2["same"]="test2" then object1["same"] value also be changes to test2.

In first case it won't happens like that, because object1 & object2 are two different objects.

refer this: Object Equality in JavaScript

hopes this is may be helpful.

share|improve this answer

In the first case Object1 and Object2 are two different reference points(or variables) pointing at two different objects in memory.so when you check them for equality the result is false.

In the second case you are just copying the reference point(address) from Object1 to Object2 meaning both the reference points are now referring the same object in memory.so the result true.

Objects are always compared by reference.

share|improve this answer

When you assign objects or arrays from one variable to another, it copies a reference to the original object, so the two objects are equal.

Each time you write an object or array literal, it makes a different object or array, so they're not equal, even if the contents are similar.

share|improve this answer

When you have created object this way, you have created a map. There is nothing like value of a map as its not of primitive types. So equals would just check for the reference equality. Hence this fails. In the second case, the reference equality goes through.

share|improve this answer

Two different object cant be equal to each other by definition in Javascript. Same attributes and values are not important. They have different pointers. (First example)

In second example, you are cloning the object with the reference. So "===" will return true.

share|improve this answer

Your example does not relate to the == vs === operator difference. As people explained before, your example simply creates two different object and in the next example reference the same. Both == and === would act the same there.

Since objects litterals are always objects litterals and not in any way implicitly convertible, == and === will always act the same for objects litterals. Some other types like NaN, null and the link, you get weird/funny results when comparing with == and ===

share|improve this answer

In your first example you created two distinct objects with arbitrary content, which is randomly the same: {same:'test'} and {same:'test'}

In your second example you created only one object, and stored it in two variables: object1 and object2

The === checks for object identity, that means the objects in the compared variables must be the same.

share|improve this answer
    
== returns false too. Try it in your browser console: ({x:'a'} == {x:'a'}) –  knittl 17 hours ago
    
You're right. Edited it. –  Waog 17 hours ago
1  
The difference between the == and === operators is that the former does type coercion, while the latter does strict comparison (type and value; the value of objects just happen to be their reference) –  knittl 17 hours ago
    
is there something predefined in JavaScript, to check for object equality? –  Waog 17 hours ago
    
Nothing predefined. You can find some user-written functions to check that. A simple workaround (which ignores functions and breaks on differently ordered fields) is to JSON-stringify the two objects and then compare the resulting strings. –  knittl 17 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.