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(+)
All mathematical operations convert values to numbers but not the addition(+) operator. In JavaScript it is used for string concatenation also. So, if one of the added values is a string then the other one is also converted to a string. Then it concatenates them:
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
Thanks for sharing the information, it is very helpful, I hope to get more such beautiful blogs from you.
You have shared great information with me i appreciate your work!
The tutorial provides the more information JavaScript this provides the end to end information about JS and also they provides the more detailed concepts of loops, functions, objects, arrays this will use us to buld the effective web site. Thanks for the team good job.
Build various types of web applications,command-line application,etc....
Introduction: Angular (What is Angular?)Angular was formerly introdu...
Leave a Reply
Your email address will not be published. Required fields are marked *