Why are two identical objects not equal to each other?

前端 未结 9 855
广开言路
广开言路 2020-11-22 05:43

Seems like the following code should return a true, but it returns false.

var a = {};
var b = {};

console.log(a==b); //returns false
console.log(a===b); //         


        
9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 06:06

    In Javascript each object is unique hence `{} == {}` or `{} === {}` returns false. In other words Javascript compares objects by identity, not by value.
    
     1. Double equal to `( == )` Ex: `'1' == 1` returns true because type is excluded 
         
     2. Triple equal to `( === )` Ex: `'1' === 1` returns false compares strictly, checks for type even
    

提交回复
热议问题