Implicit conversion
suggest changeJavaScript will try to automatically convert variables to more appropriate types upon use. It’s usually advised to do conversions explicitly (see other examples), but it’s still worth knowing what conversions take place implicitly.
"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 a Boolean
if ("hello") {} // runs, "hello" is truthy.
new Array(3) == ",,"; // true. == converts the array with toString, so the value is ",,". === is false.
Some of the trickier parts:
!"0" === false // "0" got converted to true, then reversed.
!"false" === false // "false" converted to true, then reversed.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents