1

Let us guess two objects with same property:

var x = {a : 'some'},
      y = {a: 'some'};

output:

x == y; and x === y; both give false

Similarly for two array,

var p = [1, 2, 3],
      q = [1,2,3];

p == q and p === q both give false.

But for if I do following:

var x = y = {a: 'some'};
var p = q = [1, 2, 3];

All above comparison give true.

Why Javascript do such thing? Explain Please.

3 Answers 3

5

All above comparison give true.

Right. You've specifically set p and q so they refer to the same object.

With object references, both == (if both sides are object references) and === will check to see if the references are pointing to the same object. If you have two identical, but separate, objects, both will always be false.

So for example:

var a = {}; // a points to an object
var b = {}; // b points to a _different_ object
console.log(a === b); // "false"
console.log(a == b);  // "false"

var c = {}; // c points to an object
var d = c;  // d points to _the same_ object
console.log(c === d); // "true"
console.log(c == d);  // "true"

The content of the objects is irrelevant, it's the identity of them that's being checked.

Note that this is not true if you use == and only one side is an object reference (e.g., the other side is a number, a primitive string, undefined, etc.). In that case, the object reference will be asked to convert itself (to either a string or a number, depending on what the other thing is), and then the converted result will be used for the comparison. This can lead to surprising behavior (for instance, "2" == [[[[[2]]]]] is true because the array is asked to turn itself into a string, which it does via join [which will ask its element to convert itself to a string, and so on], and you end up with "2" on the right-hand side). So I typically prefer === ("strict equality" over == ("loose equality").

Sign up to request clarification or add additional context in comments.

1 Comment

Yea, var x = {a : 'some'}, y = {a: 'some'}; are pointing to TWO different - in memory but similar - objects.
3

This happens because

var p = [1, 2, 3],
q = [1,2,3];

creates two instances of arrays. Equality operators in JS, when comparing non-primitive data, only check if they are the same instance, don't do deep check of values or properties.

With code

var p = q = [1, 2, 3];

you are creating ONE instance of the array and assign this instance to variables p and q. So, both variables store reference to the same instance, so equality operator will return true.

1 Comment

+1. More details here.
2

In this cases, the variables point to two separate objects, thus not equal.

var x = {a:'some'}, // one object with "a" = "some"
    y = {a:'some'}; // another object with "a" = "some"

var p = [1,2,3],    // one array with 1,2 and 3
    q = [1,2,3];    // another array with 1,2 and 3

But in this case, they point to the same object, thus equal

var x = y = {a: 'some'};
//is like:
var x = {a:'some'}, // x points to an object
    y = x;          // y is given reference to whatever x is pointing at


var p = q = [1,2,3];
//is like:
var p = [1,2,3],    // p points to an array
    q = p;          // q is given reference to whatever p is pointing at

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.