- By Steve Suehring
Learning about type conversions
To explicitly convert a number to a string, cast the number as a string, as in this example:
// Convert myNumString as a string with value of 100 var myNumString = String(100);
In the same way that you can convert numbers to strings, you can convert strings to numbers. You do this by casting the string as a number.
var myNumString = "100"; var myNum = Number(myNumString);
Booleans are converted to numbers automatically when used in a numeric context. The value of true becomes 1, and the value of false becomes 0. When used in a string context, true becomes “true”, and false becomes “false”. The Boolean() function exists if you need to explicitly convert a number or string to a Boolean value.