Type Conversion
String Conversion
You can call the String()
function to explicit convert a value to a string
String(false) -> "false"
String(null) -> "null"
Numeric Conversion
Occurs when you use math functions and expressions
console.log("6" / "2"); // Print out 3!
Or you can also do it more explicitly by calling theĀ Number()
function.
However, note that any non-digit character that shows up in String will result in the conversion value NaN
console.log(Number("lol")); // Results in NaN
Here is the table of conversion for some common values passed into Number()
Value |
Becomes |
undefined |
NaN |
null |
0 |
true and false |
1 and 0 |
string |
Empty string is 0, then convert the string to number, if any non-digit character are present results in NaN |
Boolean Conversion
Like mentioned before, any number that's besides 0 are considered to be true.
You can call the Boolean()
function to explicitly convert a value to be either true/false
Non-empty strings are also considered to be true so even "0"
is considered to be true.
No Comments