Triple equal signs return false for arrays in javascript. why?

后端 未结 4 1723
南方客
南方客 2020-11-27 19:15

I know that === is typically referred to as the identity operator. Values being compared must be of the same type and value to be considered equal. Then why bel

4条回答
  •  [愿得一人]
    2020-11-27 19:47

    First of all === is strict equality, not an identity operator, and Arrays, like objects are reference objects, not value objects as in the case of numbers and strings...

    So when you are comparing those 2 arrays, you are creating two different arrays in memory. It'd be the same as saying...

    var x = { a: 1 }; 
    var y = { a: 1 };
    
    x === y; //false  
    

    You can't directly compare arrays or objects like that.

提交回复
热议问题