Leila Hamon
Leila Hamon

Reputation: 2595

Looking for an explanation of why `[1,2,3] === [1,2,3]` is false in JS

I was looking at clause 11.9.6 of ES5 trying to figure out why [1,2,3] === [1,2,3] returns false.

The code:

a = [1,2,3]
b = [1,2,3]
a === b // false

Relevant rules for the strict equality comparison algo:

x === y

(1) If Type(x) is different from Type(y), return false.

...

...

...

(7) Return true if x and y refer to the same object. Otherwise return false.

Any ideas why an interpreter returns false?

Upvotes: 1

Views: 197

Answers (5)

Bort
Bort

Reputation: 7618

a and b are two different objects, they just happen to have the same Number values in them. If you did a[0] = 42; then b[0] would still equal 1.

Upvotes: 4

Ed Heal
Ed Heal

Reputation: 59997

Because there are two objects created.

Upvotes: 0

Dan Barzilay
Dan Barzilay

Reputation: 4983

(7) Return true if x and y refer to the same object. Otherwise return false. -they doesn't refer to the same object.

Upvotes: 0

quamrana
quamrana

Reputation: 39354

It looks like your a and b refer to different objects. They might each contain the same value and are instances of the same class, but you can change one and the other stays the same.

Upvotes: 0

Florian Minges
Florian Minges

Reputation: 606

They do not refer to the same object. Simple as that.

Upvotes: 0

Related Questions