Table of Contents

Type Conversion in JavaScript

Code Playground is only enabled on larger screen sizes.

Automatic conversion

JavaScript is a weak type programming language. It will always try to execute the code, regardless of the given value's data types, even if it does not make sense.

For example:

javascript
console.log(1 + "1"); // -> 11

In this case, JavaScript assumes you are trying to perform a concatenation operation, and it will convert the number 1 into a string, and then join the two strings together.

And in the following example, null will be converted into the number 0, and 5 times 0 gives 0.

javascript
console.log(5 * null); // -> 0

Even though these examples technically work, you should never rely on automatic conversions, as they can lead to unexpected problems.

A better way is to convert the values to the right types explicitly, in order to ensure they are always the intended types.

Convert to strings

For example, if you intend to perform string concatenation, then make sure all the values are converted into strings using the String() function.

javascript
console.log(typeof String(null)); // -> string
console.log(typeof String(undefined)); // -> string
console.log(typeof String(true)); // -> string
console.log(typeof String(false)); // -> string
console.log(typeof String(123)); // -> string
console.log(typeof String("123")); // -> string

Boolean values and numbers also have a built-in toString() method that does the same thing.

javascript
console.log(typeof true.toString()); // -> string
console.log(typeof false.toString()); // -> string
console.log(typeof (123).toString()); // -> string

The difference between String() and toString() is that String() is global. It works anywhere and it works on null and undefined as well.

javascript
console.log(typeof String(null)); // -> string
console.log(typeof String(undefined)); // -> string

toString() is tied to specific data types, meaning the toString() for numbers and the toString() for Boolean values are not the same.

And they are not implemented for null and undefined in JavaScript. The following code will cause an error:

javascript
console.log(typeof null.toString()); // -> Error

Convert to numbers

The Number() function can be used to convert other data types into numbers. The conversion obeys the following rules:

  • Strings that contain only white spaces will be converted into 0.
javascript
console.log(Number("\n\t   ")); // -> 0
  • Strings that can be converted into numbers without errors, meaning they contain only number digits or a sign in the front, will be converted into the corresponding number.
javascript
console.log(Number("00001")); // -> 1
console.log(Number("12345")); // -> 12345
console.log(Number("-12345")); // -> -12345
console.log(Number("-0.001")); // -> -0.001
  • The leading and tailing white spaces will be automatically removed.
javascript
console.log(Number("\t00001\t"));
console.log(Number("12345\n"));
text
1
12345
  • If an error is encountered during conversion, meaning one or more characters cannot be converted, then it will return NaN.
javascript
console.log(Number("000a01")); // -> NaN
console.log(Number("123 45")); // -> NaN
console.log(Number("-12-345")); // -> NaN
console.log(Number("-0.00.1")); // -> NaN
  • For Boolean values, true will become 1, and false will become 0.
javascript
console.log(Number(true)); // -> 1
console.log(Number(false)); // -> 0
  • null will become 0, and undefined will become NaN.
javascript
console.log(Number(null)); // -> 0
console.log(Number(undefined)); // -> NaN

This is one of the few differences between null and undefined.

Convert to Boolean values

The Boolean() function is used to convert other data types into either true or false.

All falsy values will become false:

javascript
console.log(Boolean(null)); // -> false
console.log(Boolean(undefined)); // -> false
console.log(Boolean(0)); // -> false
console.log(Boolean("")); // -> false

All truthy values will become true:

javascript
console.log(Boolean(10)); // -> true
console.log(Boolean(-10)); // -> true
console.log(Boolean(" ")); // -> true
console.log(Boolean("qwerty")); // -> true