隐式转换
JavaScript 将尝试在使用时自动将变量转换为更合适的类型。通常建议明确地进行转换(参见其他示例),但仍然需要知道隐式转换发生了什么。
"1" + 5 === "15" // 5 got converted to string.
1 + "5" === "15" // 1 got converted to string.
1 - "5" === -4 // "5" got converted to a number.
alert({}) // alerts "[object Object]", {} got converted to string.
!0 === true // 0 got converted to boolean
if ("hello") {} // runs, "hello" got converted to boolean.
new Array(3) === ",,"; // Return true. The array is converted to string - Array.toString();
一些棘手的部分:
!"0" === false // "0" got converted to true, then reversed.
!"false" === false // "false" converted to true, then reversed.