JavaScript Type Conversions
JavaScript Type Conversion can automatically convert values from one data type to another. Whenever it tries to operate on a "wrong" data type, it will try to convert the value to a "right" type.
Observe the example below to understand different results for doing operations over different data types.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Automatic Type Conversion </title>
</head>
<body>
<script>
document.write("3" - 2);
document.write("<br>");
document.write("3" + 2);
document.write("<br>");
document.write(3 + "2");
document.write("<br>");
document.write("3" * "2");
document.write("<br>");
document.write("10" / "2");
document.write("<br>");
document.write(1 + true);
document.write("<br>");
document.write(1 + false);
document.write("<br>");
document.write(1 + undefined);
document.write("<br>");
document.write(3 + null);
document.write("<br>");
document.write("3" + null);
document.write("<br>");
document.write(true + null);
document.write("<br>");
document.write(true + undefined);
</script>
</body>
</html>
Output
JavaScript Automatic Type Conversion Examples
Conversion By Functions
Convert Value to Number
The Number()
function can change a string to a number. It is used when we expect a number to be entered in the text input. String like "57", will be converted to 57. If there is an empty string, it will get converted to 0 and anything else like "23hg", "Ansh", etc. will get converted to NaN(Not a Number).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> JavaScript Convert a Numeric String to Number</title>
</head>
<body>
<script>
var str = "123";
document.write(typeof str); // Outputs: string
document.write("<br>");
var num = Number(str); // Becomes a number 123
document.write(typeof num); // // Outputs: number
</script>
</body>
</html>
Output
JavaScript Convert a Numeric String to Number
The Unary + Operator
The unary + operator can also be used to convert a variable to a number.
Same as above, if the variable cannot be converted, then its type will converted to NaN.
Converting Numbers to String
String()
method is used to convert numbers to a string.
There is one more method toString()
, which can also be used to convert numbers to string.
Boolean to String
The same methods String()
and toString()
methods can also be used to change the type of boolean values to String.
Below is the example, where a boolean value is converted to string. You can change the functions to toString()
in the Try-it editor to see that both the functions works the same way.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> JavaScript Convert a Value to String </title>
</head>
<body>
<script>
var bool = true;
document.write(typeof bool); // Prints: boolean
document.write("<br>");
var str = String(bool); // Becomes a string "true"
document.write(typeof str); // Prints: string
</script>
</body>
</html>
Output
JavaScript Convert a Value to String
Date to String
The String()
and toString()
methods can be used to convert dates to strings too.