Some examples of type conversion are converting from String data to Number, Number to String, etc.
Types of type conversions
- Implicit type conversion - This means that the conversion happens automatically
- Explicit type conversion - This means that the conversion has to be done manually by the user.
Implicit type conversion
Automatically??. Let’s understand this through some examples.
let answer; answer= 1 + 2; console.log(answer)
So far, so good. Now let’s try adding a numeric value and a string together.
let answer; answer= 1 + 'Ninja'; console.log(answer)
let answer; answer = 1 + 'Ninja'; console.log(answer); console.log(typeof(answer));
Explicit type conversion
Imagine we need to convert a boolean value or a numeric value to a string. How can we do this? Let’s see.
let number='777'; let answer; // Type conversion from String to number console.log(number); console.log(typeof(number)); // Using Number() function to do the conversion answer = Number(number); console.log(answer); console.log(typeof(answer));
777 string 777 number
Here we have converted the string value to a number using the Number() function. Since we had to do this conversion for our own requirement, we call these explicit type conversions.
We do string conversion when we need the string form of a value. We use the String() function to convert a value to a string.
let input = false; console.log("input = "+input); console.log(typeof(input)); input = String(input); console.log("input = "+input); console.log(typeof(input));
input = false boolean input = false string
Numeric conversion can happen implicitly when we try to do mathematical operations on strings. We can also do numeric conversions explicitly.
Code showing implicit conversion:
let a = "9"; let b = "3"; console.log(a/b);
Explicit conversions to numeric data types can be done using the Number() function.
Code showing explicit type conversion:
let a = "9"; console.log(a); console.log(typeof(a)); a = Number(a); console.log(a); console.log(typeof(a));
9 string 9 number
Numeric conversion rules
|string||White spaces are stripped off. If the resulting string is empty, then the answer is 0. Otherwise, the number is read from the string. Errors give NaN.|
console.log(Number("777")); console.log(Number("404ninja")); console.log(Number(true)); console.log(Number(false));
777 NaN 1 0
Boolean Type Conversion
Boolean type conversion is a relatively simple conversion. We can use the Boolean() function to convert to boolean data type.
We have to note that empty values like 0, empty strings, null, undefined, NaN, etc. become false. Other values become true.
console.log(Boolean(1)); console.log(Boolean(0)); console.log(Boolean("Ninja")); console.log(Boolean("")); console.log(Boolean(NaN)); console.log(Boolean(null));
true false true false false false
|Value||String Conversion||Number Conversion||Boolean Conversion|
|' '||" "||0||true|
Frequently Asked Questions
1. What are the ways to convert dates to strings?
Ans:- The String() global method can convert dates to strings. The method toString() also does the same thing. Use them as String(Date()) and Date().toString() respectively.
2. What is the use of the parseInt() method?
Ans:- The parseInt() method parses a string and returns an integer.
3. What purpose does the toPrecision() method serve?
Ans:- The toPrecision() method returns a string, with a number written with a specified length.