0

Possible Duplicate:
How do you determine equality for two JavaScript objects?

Why does [1,[2,3]] == [1,[2,3]] evaluate to false?

Also, why does this happen:

var g = { a:1, b:2, c:3 };
g == { a:1, b:2, c:3 }; // false!! 
Community
  • 1
  • 1
PitaJ
  • 12,969
  • 6
  • 36
  • 55
  • @RobertHarvey why? `[1,[2,3]] === [1,[2,3]]` is also `false`. – kojiro Jul 31 '12 at 21:17
  • @RobertHarvey - It's still false – PitaJ Jul 31 '12 at 21:17
  • @kojiro thats because they are not the same object. – Naftali Jul 31 '12 at 21:19
  • 1
    @RobertHarvey, I'd say that's _even more false_ if there is such a thing. :) – sblom Jul 31 '12 at 21:19
  • The algorithm for the equals operator is defined in [ECMA-262](http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf), Section 11.9.3. It all boils down to rule 1. f.: "1. If Type(*x*) is the same as Type(*y*), then […] f. Return **true** if *x* and *y* refer to the same object. Otherwise, return **false**." You are comparing two objects, which might have the same content - yet are not referring to the same object. – reima Jul 31 '12 at 21:22
  • As Zack mentions below in one of the answer's comments, you can use the JSON.stringify method to compare two object's values. This however is only backwards compatible so far unless you add the standard JSON javascript library to your project. `JSON.stringify({a:1}) == JSON.stringify({a:1})` – dqhendricks Jul 31 '12 at 21:29
  • It's like in Java and probably other OO languages: Comparing two objects using the equals operator only results in true if both reference the same object. Why does this confuse people? – Felix Kling Jul 31 '12 at 21:55

2 Answers2

3

Because [] and {} creates new instances of objects and they are not equal.

micnic
  • 10,915
  • 5
  • 44
  • 55
3

[] is a shortcut to make an array literal instead of calling new Array() and then populating it. It's a similar story for {}. In your example, you are actually comparing by reference instead of by value. Two objects constructed with the new operator point to different locations in memory, and when you use the == operator you are actually saying "do these objects point to the same location in memory?". To do a by-value comparison like you're expecting, you would need to iterate through the members of each array/object you're comparing and compare each value one-by-one.

Zach Shipley
  • 1,092
  • 6
  • 5
  • Is there a way to get them to be equal? – PitaJ Jul 31 '12 at 21:19
  • See http://stackoverflow.com/questions/201183 – Robert Harvey Jul 31 '12 at 21:24
  • @PitaJ Yes, convert your object to a json and compare. http://jsfiddle.net/VzA55/ – Ricardo Alvaro Lohmann Jul 31 '12 at 21:25
  • 1
    @PitaJ They would have to be *the same object* to be truly equal. Meaning they were constructed by the same call. Now, if you want to test for value equality you *can* do that. One easy (not the most performant) way is to convert them both to Strings. `JSON.stringify(g) === JSON.stringify({a:1, b:2, c:3})` is `true` and is in-line with your expectations. – Zach Shipley Jul 31 '12 at 21:27