隱式轉換

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.