Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am working on a part of the code where I have an array which looks like [[data]]. The data is rendered on the server side through the Django template engine. So my code looks like this:

var data = {{ series|safe }}; 
// data will be [[]] if no data is present
if (data ==[[]])
  console.log('no data');

The if always returns false. That means in [[]] == [[]] is false and my test shows that []==[] is false as well.

Any descriptions would be appreciated.

like image 410
Nasir Avatar asked Dec 04 '12 12:12

Nasir


People also ask

Why is array === array false?

So there is no way that two arrays, or any object, can come out === being true unless they point to the exact same array. Just because two objects are being created with identical contents does not mean that they point to the same object, just two identical, but different objects.

Why do false == [] and false == ![] Both return true?

Types of x and y are checked when x == y is to be checked. If no rule matches, a false is returned. For [] == true , rule 7 matches, so a result of [] == ToNumber(true) is returned i.e. false is returned. Reason you're getting the same result for ![]

Can you explain why `{} == {}` is false in JavaScript?

{} creates an new object. The comparison operator ==, when used with objects, checks whether two objects are the same one. Since there are two {} in the statement {} == {}, two new objects are created separately, and then they are compared. Since they are not the same object, the result is false.

Why {} === {} is equal to false?

Because == and === check if the two compared variables are references to the same object, not whether they are identical in value. Thus a two variables internally referencing each other or a third variable are both == and === , two new instances of an object are not.


2 Answers

Because [] creates a new array, so you are comparing one array object with another array object.

It's not the contents of the arrays that is compared, the object references are compared. They are not equal because it's not the same object instance.

like image 144
Guffa Avatar answered Oct 20 '22 08:10

Guffa


Because Arrays are reference type, meaning, if for example you make an array

let a = [1,2,3,4,5];

let b = a;

the b is actually just a reference of array a, so if you compare them

a===b is true,

because they are basically link together.. So if you change something to array b it will also going to be change to array a,

b[0] = "test";

array a now is ["test",2,3,4,5];

But if you do this this

let a = [1,2,3,4,5];

let b = a.slice(0);

and then compare them

a===b is false

because now they are both different Arrays, meaning if you change the Array b, it will not affect the Array a

b[0] ="hello";

Array a is still [1,2,3,4,5]

while array b is now ["hello",2,3,4,5]

that is also what happen when you compare the []===[] is false

Because basically what you are asking to JavaScript is if they are the same Array which is not

like image 24
Richard Ramos Avatar answered Oct 20 '22 06:10

Richard Ramos