JavaScript - Type Conversions
JavaScript - Type Conversions
JavaScript Type Conversions
Type Conversionsin JavaScript refer to the automatic or explicit process of converting data from one data type to another in JavaScript. These conversions are essential for JavaScript to perform operations and comparisons effectively. JavaScript variables can contain the values of any data type as it is a weakly typed language.
There are two types of type conversion in JavaScript β
- Implicittype conversion
- Explicittype conversion
The implicit type conversion is also known ascoercion.
Implicit Type Conversion
When type conversion is done by JavaScript automatically, it is calledimplicit typeconversion. For example, when we use the β+β operator with thestringandnumberoperands, JavaScript converts thenumberto astringand concatenates it with the string.
Here are some examples of the implicit type conversion.
Converting to String (Implicit conversion)
In this example, we used the β+β operator to implicitly convert different values to the string data type.
"100" + 24; // Converts 24 to string
'100' + false; // Converts false boolean value to string
"100" + null; // Converts null keyword to stringPlease note that to convert a value to string using β+β operator, one operand should be string.
Letβs try the example below, and check the output β
Converting to Number (Implicit conversion)
When you use the string values containing the digits with the arithmetic operators except for the β+β operator, it converts operands to numbers automatically and performs the arithmetic operation, which you can see in the example below.
Boolean values are also gets converted to a number.
'100' / 50; // Converts '100' to 100
'100' - '50'; // Converts '100' and '50' to 100 and 50
'100' * true; // Converts true to 1
'100' - false; // Converts false to 0
'tp' / 50 // converts 'tp' to NaNTry the example below and check the output β
Converting to Boolean (Implicit conversion)
When you use the Nullish (!!) operator with any variable, it implicitly converts its value to thebooleanvalue.
num = !!0; // !0 = true, !!0 = false
num = !!1; // !1 = false, !!1 = true
str = !!""; // !"" = true, !!"" = false
str = !!"Hello"; // !"Hello" = false, !!"Hello" = trueNull to Number (Implicit conversion)
In JavaScript, thenullrepresents the empty. So,nullautomatically gets converted to0when we use it as an operand of the arithmetic operator.
let num = 100 + null; // Converts null to 0
num = 100 * null; // Converts null to 0Undefined with Number and Boolean (Implicit conversion)
Using theundefinedwith the βnumberβ or βbooleanβ value always gives theNaNin the output. Here, NaN means not a number.
Explicit Type Conversion
In many cases, programmers are required to convert the data type of the variable manually. It is called theexplicit typeconversion.
In JavaScript, you can use theconstructorfunctions orbuilt-infunctions to convert the type of the variable.
Converting to String (Explicit conversion)
You can use theString()constructor to convert the numbers, boolean, or other data types into the string.
String(100); // number to string
String(null); // null to string
String(true); // boolean to stringExample
You can use the String() constructor function to convert a value to the string.You can also usetypeofoperator to check the type of the resultant value.
We can also use thetoString()method of Number object to convert number to string.
const num = 100;
num.toString() // converts 100 to '100'Converting to Number (Explicit conversion)
You can use theNumer()constructor to convert a string into a number. We can also useunary plus (+)operator to convert a string to number.
Number('100'); // Converts '100' to 100
Number(false); // Converts false to 0
Number(null); // Converts null to 0
num = +"200"; // Using the unary operatorHowever, you can also use the below methods and variables to convert the string into numbers.
Sr.No.Method / OperatorDescription1parseFloat()To extract the floating point number from the string.2parseInt()To extract the integer from the string.3+It is an unary operator.
Example
You can use the Number() constructor function or unary (+) operator to convert a string, boolean, or any other value to a number.
The Number() function also converts the exponential notation of a number to a decimal number.
Converting to Boolean (Explicit conversion)
You can use theBoolean()constructor to convert the other data types into Boolean.
Boolean(100); // Converts number to boolean (true)
Boolean(0); // 0 is falsy value (false)
Boolean(""); // Empty string is falsy value (false)
Boolean("Hi"); // Converts string to boolean (true)
Boolean(null); // null is falsy value (false)Example
You can use the Boolean() constructor to convert values to the Boolean. All false values like 0, empty string, null, undefined, etc., get converted tofalseand other values are converted totrue.
Converting Date to String/Number
You can use the Date objectβs Number() constructor orgetTime()method to convert the date string into the number. The numeric date represents the total number of milliseconds since 1st January 1970.
Follow the syntax below to convert the date into a number.
Number(date);
OR
date.getTime();You can use theString()constructor or thetoString()method to convert the date into a string.
Follow the syntax below to convert the date into the string.
String(date);
OR
date.toString();Letβs try to demonstrate this with the help of a program.
Conversion Table in JavaScript
In the below table, we have given the original values and their resultant value when we convert the original value to string, number, and boolean.
ValueString conversionNumber conversionBoolean conversion0β0β0false1β1β1trueβ1""1β1trueβ0""0β0true""""0falseβHello""HelloβNaNtruetrueβtrueβ1truefalseβfalseβ0falsenullβnullβ0falseundefinedβundefinedβNaNfalse[50]β50β50true[50, 100]β[50, 100]βNaNtrue