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

前端 未结 6 1733
时光说笑
时光说笑 2020-12-03 17:32

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 temp

6条回答
  •  遥遥无期
    2020-12-03 18:23

    Javascript is like Java in that the == operator compares the values of primitive types, but the references of objects. You're creating two arrays, and the == operator is telling you that they do not point to the same exact object in memory:

    var b = new Array( 1, 2, 3 );
    var c = new Array( 1, 2, 3 );
    
    console.log(b == c); // Prints false.
    console.log(b == b); // Prints true.
    console.log(b === c); // Prints false.
    
    b = c;
    
    console.log(b == c); // Now prints true.
    

    You have to do a deep comparison by hand if you want to compare the values of the objects.

提交回复
热议问题