33

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sanman
  • 770
  • 1
  • 7
  • 15
  • 3
    Objects are always compared by reference, not equality – Willem D'Haeseleer Aug 05 '14 at 06:19
  • Removed incorrect "duplicate" flag. – Niet the Dark Absol Aug 05 '14 at 18:45
  • 3
    @NiettheDarkAbsol Incorrect duplicate flag? There are tons of similar questions on SO (by example this one, which is also flagged as duplicate: http://stackoverflow.com/questions/1068834/object-comparison-in-javascript?lq=1) What's the difference with this one? – Getz Aug 06 '14 at 08:53
  • Well, it's not really the same question. In your link the OP asks "I know that 'Two objects are equal if they refer to the exact same Object', but is there a way to check it another way??". Whereas in this post, the OP does not know that 'Two objects are equal if they refer to the exact same Object', and this is what he asks. – dgiugg Aug 06 '14 at 09:12
  • 1
    If you want another example, this is exactly the same question: http://stackoverflow.com/questions/22828341/why-are-two-objects-with-the-same-values-not-equal – Getz Aug 06 '14 at 09:44
  • Mh, you are right, this one is pretty much the same question... But I think @NiettheDarkAbsol's answer is really a good one: it's short, true and really understandable. But I guess that if this question is marked as duplicate, the answer will remain. – dgiugg Aug 06 '14 at 09:49
  • @Getz The question was previously closed as a duplicate of [*two* other questions](http://stackoverflow.com/questions/201183/), but one of them was completely irrelevant (it was on [the difference between `==` and `===`](http://stackoverflow.com/questions/359494/)). I therefore performed a double-swing of Mjölnir to re-open the question, and then re-close it as a duplicate of the correctly flagged duplicate. However, it would seem that this question was [reopened again by popular vote](http://stackoverflow.com/posts/25132255/revisions). – Niet the Dark Absol Aug 06 '14 at 11:31
  • Such a simple question is of course an exact duplicate 6 years after Stack Overflow was launched. It is just a matter of finding a suitable duplicate. – Peter Mortensen Aug 09 '14 at 11:22

10 Answers10

83

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.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 31
    Good analogy. Also consider what happens to ball1 if we decide to paint ball2 green. – tobyink Aug 05 '14 at 14:41
  • 1
    Excellent analogy (I'm totally stealing it) :) – Dimitar Dimitrov Aug 06 '14 at 05:27
  • +1. Bonus points for explaining how I can ask "Do these two balls look and behave the same?" – knittl Aug 06 '14 at 05:38
  • 2
    Applies to many languages. Not only java script. This post should be added to Stackoverflow wiki :D – Suresh Atta Aug 06 '14 at 08:38
  • That's a decent analogy, but it might help to also explain what the computer is actually doing when assigning values to variables so the OP can learn more about what's going on under the hood of their two examples. – beporter Aug 22 '14 at 22:12
36

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)

kapa
  • 77,694
  • 21
  • 158
  • 175
knittl
  • 246,190
  • 53
  • 318
  • 364
9
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.

Srinu Chinka
  • 1,471
  • 13
  • 19
6

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.

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36
4

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
3

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.

biplav
  • 781
  • 6
  • 13
3

Two different object can't be equal to each other by definition in JavaScript. Equal attributes and values are not important. They have different pointers (first example).

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmet Can Güven
  • 5,392
  • 4
  • 38
  • 59
2

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.

Waog
  • 7,127
  • 5
  • 25
  • 37
  • `==` returns false too. Try it in your browser console: `({x:'a'} == {x:'a'})` – knittl Aug 05 '14 at 06:22
  • 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 Aug 05 '14 at 06:26
  • is there something predefined in JavaScript, to check for object equality? – Waog Aug 05 '14 at 06:28
  • 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 Aug 05 '14 at 06:32
  • @NathanCooper: yes, that's why I noted that it "ignores functions and breaks on differently ordered fields" – knittl Aug 05 '14 at 14:37
  • @knittl ahh carry on then. I'll just delete my stupid comment. – Nathan Cooper Aug 05 '14 at 15:03
  • 1
    +1 since this is the only answer containing the word *identity*. – DaoWen Aug 06 '14 at 04:33
2

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 like, you get weird/funny results when comparing with == and ===

Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101
0

This is how javascript handling and comparing objects:

enter image description here

Mohammed Alawneh
  • 306
  • 9
  • 22