Why is [] !== [] in JavaScript?

fahrradflucht picture fahrradflucht · Oct 28, 2016 · Viewed 10.4k times · Source

Why is [] !== [] in JavaScript?

I read through https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness but I could not find anything that explains this.

Edit: I don't think this question or this question is an exact duplicate of mine. It asks about the == operator which just behaves crazy. The answer is an answer to my question but it's not the same question.

Answer

Lew picture Lew · Oct 28, 2016

That does a reference check on the two array literals to see if they are the same instance. The fact that you have two literals means that you are constructing two separate arrays, therefore the reference check returns false. This would return true:

var a = []
var b = a

//b === a

This is because we have two references to the same array.