咱们会在一些代码中看到遇到“===”和“==”的写法,三等号与双等号有什么不一样吗?在JavaScript中,===表示“恒等于”,==表示“等于”。=表示“赋值”。 ip
运行体会一下如下的代码就会清楚了:
alert(0 == ""); // true
alert(0 == false); // true
alert("" == false); // true
alert(0 === ""); // false
alert(0 === false); // false
alert("" === false); // false 语言
在复杂一些: 类型转换
JavaScript 是弱类型语言,这就意味着,等于操做符会为了比较两个值而进行强制类型转换。注意0的相等运算,例如:
"" == "0" // false
0 == "" // true
0 == "0" // true
false == "false" // false
false == "0" // true
false == undefined // false
false == null // false
null == undefined // true
" \t " == 0 // true background
而恒等于不像普通的等于操做符,不会进行强制类型转换。这样的话上述的结果就不太相同了:
"" === "0" // false
0 === "" // false
0 === "0" // false
false === "false" // false
false === "0" // false
false === undefined // false
false === null // false
null === undefined // false
" \t " === 0 // false undefined