fahrradflucht fahrradflucht - 8 months ago 47
Javascript Question

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

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.

I don't think 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.

Lew Lew

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.