Most of the time, operators and functions automatically convert a value to the correct type. That’s called “type conversion”.
For example, alert automatically converts any value to a string to show it. Mathematical operations convert values to numbers.
There are also cases when user need to explicitly convert a value to put things right.
String conversion happens when we need the string form of a value.
For example, alert(value) does it to show the value. The conversion here is done automatically. We can also use a call String(value) function for that conversion manually.
let value = true; alert(typeof value); // boolean value = String(value); // now value is a string "true" alert(typeof value); // string
String conversion is mostly obvious. A true becomes "true", 0 becomes "0".
Numeric conversion happens in mathematical functions and expressions automatically.
For example, when division / is applied to non-numbers, which can be converted:
console.log("12"/"2"); //6 – Both strings are converted to Number
We can use a Number(value) function to explicitly convert a value to Number. Like below:
let str = "123"; let num = Number(str); console.log(typeof str); //string console.log(typeof num); //number
If the string to be converted is not a valid number, the result of such conversion is NaN, for example:
let str = "One two three"; let num = Number(str); console.log(num); //NaN
Some other numeric conversion rules for explicit conversation are below
//undefined becomes null let str; let num = Number(str); console.log(str); //undefined console.log(num); //NaN //null becomes 0 let str = null; let num = Number(str); console.log(str); //null console.log(num); //0 //true/false becomes 1/0 let str1 = true; let str2 = false; let num1 = Number(str1); let num2 = Number(str2); console.log(str1); //true console.log(num1); //1 console.log(str2); //false console.log(num2); //0 //In strings whitespaces from the start and the end are removed. Then, if //the remaining string is empty, the result is 0. Otherwise, the number is //“read” from the string. An error gives NaN if the string cannot be //converted. let str1 = " "; let str2 = " 123 "; let str3 = " 123z "; let num1 = Number(str1); let num2 = Number(str2); let num3 = Number(str3); console.log(num1); //0 console.log(num2); //123 console.log(num3); //NaN
Problem with addition operator(+)
console.log(1 + "2"); //12 //1 gets converted to string and so "1" + "2" becomes "12" console.log(4 + 5 + "3"); //93 //First 4 + 5 gives 9. Since "3" is string, so 9 is converted to "9" and //the result becomes "93" console.log("3" + 5 + 4); //354 //As with above cases "3" + 5 gives "35" and then "35" + 4 gives "354"
Boolean conversion is very simple. It follows two basic rules.
console.log(Boolean(0)); //false console.log(Boolean(1)); //true console.log(Boolean("0")); //true – since non-empty string console.log(Boolean("")); //false console.log(Boolean(undefined)); //false console.log(Boolean(null)); //false console.log(Boolean(NaN)); //false
You have shared great information with me i appreciate your work!