JavaScript - Type Conversions
JavaScript - Type Conversions
Please note that to convert a value to string using "+" operator, one operand should be string.
<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.
<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>
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.