JavaScript - Type Conversions
JavaScript Type Conversions
Type Conversions in 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 −
Implicit type conversion
Explicit type conversion
The implicit type conversion is also known as coercion.
Implicit Type Conversion
When type conversion is done by JavaScript automatically, it is called implicit type conversion. For example, when we
use the '+' operator with the string and number operands, JavaScript converts the number to a string and 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 string
Please 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 −
<html>
<head>
<title>Implicit conversion to string </title>
Converting to Boolean (Implicit conversion)
When you use the Nullish (!!) operator with any variable, it implicitly converts its value to the boolean value.
num = !!0; // !0 = true, !!0 = false
num = !!1; // !1 = false, !!1 = true
str = !!""; // !"" = true, !!"" = false
str = !!"Hello"; // !"Hello" = false, !!"Hello" = true
Null to Number (Implicit conversion)
In JavaScript, the null represents the empty. So, null automatically gets converted to 0 when we use it as an operand of
the arithmetic operator.
let num = 100 + null; // Converts null to 0
num = 100 * null; // Converts null to 0
Undefined with Number and Boolean (Implicit conversion)
Using the undefined with the 'number' or 'boolean' value always gives the NaN in the output. Here, NaN means not a
number.
<html>
<head>
<title> Using undefined with a number and boolean value </title>
</head>
<body>
<script>
let num = 100 + undefined; // Prints NaN
document.write("The value of the num is: " + num + "<br>");
num = false * undefined; // Prints NaN
document.write("The value of the num is: " + num + "<br>");
</script>
</body>
</html>
Explicit Type Conversion
In many cases, programmers are required to convert the data type of the variable manually. It is called the explicit type
conversion.
In JavaScript, you can use the constructor functions or built-in functions to convert the type of the variable.
Converting to String (Explicit conversion)
You can use the String() 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 string
Example
You can use the String() constructor function to convert a value to the string.You can also use typeof operator to check
the type of the resultant value.
<html>
<head>
<title> Converting to string explicitly </title>
</head>
<body>
<script>
document.write(typeof String(100) + "<br/>");
document.write(typeof String(null)+ "<br/>");
document.write(typeof String(true) + "<br/>");
</script>
</body>
</html>
We can also use the toString() 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 the Numer() constructor to convert a string into a number. We can also use unary plus (+) operator to convert
a string to number.
Number('100'); // Converts '100' to 100
Number(false); // Converts false to 0