When would JavaScript == make more sense than ===?

后端 未结 4 2013
太阳男子
太阳男子 2021-01-01 17:22

As Which equals operator (== vs ===) should be used in JavaScript comparisons? indicates they are basically identical except \'===\' also ensures type equality

4条回答
  •  爱一瞬间的悲伤
    2021-01-01 17:53

    == compares whether the value of the 2 sides are the same or not.

    === compares whether the value and datatype of the 2 sides are the same or not.

    Say we have

    $var = 0;
    
    if($var == false){
      // true because 0 is also read as false
    }
    
    if(!$var){
      // true because 0 is also read as false
    }
    
    if($var === false){
      // false because 0 is not the same datatype as false. (int vs bool)
    }
    
    if($var !== false){
      // true becuase 0 is not the same datatype as false. (int vs bool)
    }
    
    if($var === 0){
      // true, the value and datatype are the same.
    }
    

    you can check http://www.jonlee.ca/the-triple-equals-in-php/

提交回复
热议问题