JavaScript allows variables to be converted from one data type to another. This can be done by:
- Explicitly using JavaScript functions
- Implicitly by JavaScript itself
Types of Conversion
- Converting Strings to Numbers
- Converting Numbers to Strings
- Converting Dates to Numbers
- Converting Numbers to Dates
- Converting Booleans to Numbers
- Converting Numbers to Booleans
Converting Strings to Numbers
The global Number()
function converts a variable or value into a number.
- Numeric strings (e.g.,
"3.14"
) convert to numbers (3.14
). - An empty string (
""
) converts to0
. - A non-numeric string (
"John"
) converts toNaN
(Not a Number).
Example
console.log(Number("3.14")); // 3.14
console.log(Number(Math.PI)); // 3.141592653589793
console.log(Number(" ")); // 0
console.log(Number("")); // 0
console.log(Number("99 88")); // NaN
console.log(Number("John")); // NaN
The Unary +
Operator
The unary +
operator can also be used to convert a string into a number.
let y = "5"; // y is a string
let x = +y; // x is a number
console.log(x); // 5
If the string cannot be converted, it results in NaN
let y = "John";
let x = +y;
console.log(x); // NaN
Converting Numbers to Strings
The String()
function converts numbers to strings.
console.log(String(123)); // "123"
console.log(String(100 + 23)); // "123"
The toString()
method does the same:
let x = 123;
console.log(x.toString()); // "123"
console.log((100 + 23).toString()); // "123"
Converting Dates to Numbers
Use the Number()
function to convert a date to milliseconds since January 1, 1970.
let d = new Date();
console.log(Number(d)); // e.g., 1678945200000
The getTime()
method does the same:
console.log(d.getTime()); // e.g., 1678945200000
Converting Dates to Strings
Use the String()
function to convert a date to a string.
console.log(String(new Date())); // e.g, Thu Mar 20 2025 15:48:20 GMT+0500
The toString()
method also works:
console.log(new Date().toString()); // e.g, Thu Mar 20 2025 15:49:53 GMT+0500
Converting Booleans to Numbers
Using Number()
: false
converts to 0 and true
converts to 1.
console.log(Number(false)); // 0
console.log(Number(true)); // 1
Converting Booleans to Strings
Using String()
: true
and false
convert to string representations.
console.log(String(false)); // "false"
console.log(String(true)); // "true"
The toString()
method works similarly
console.log(false.toString()); // "false"
console.log(true.toString()); // "true"
Automatic Type Conversion
JavaScript automatically converts types when needed. However, results may be unexpected:
console.log(5 + null); // 5 (null converts to 0)
console.log("5" + null); // "5null" (null converts to "null")
console.log("5" + 2); // "52" (number 2 converts to "2")
console.log("5" - 2); // 3 ("5" converts to 5)
console.log("5" * "2"); // 10 (both convert to numbers)
Automatic String Conversion
JavaScript calls toString()
when a value is expected as a string:
let obj = { name: "John" };
console.log(obj.toString()); // "[object Object]"
let arr = [1, 2, 3, 4];
console.log(arr.toString()); // "1,2,3,4"
Numbers and booleans are also automatically converted:
console.log(123 + ""); // "123"
console.log(true + ""); // "true"
console.log(false + ""); // "false"