Learn JavaScript
Type Conversions

Converting to Number

We can use the Number() method to convert a non-number value to a number.

String to Number

A String which contains a valid number as its value will be converted to a Number.

Empty strings are converted to 0.

Non-empty Strings which don’t contain a valid number are converted to NaN.

Boolean to Number

true is converted to 1, while false is converted to 0.

Other data types to Number

null is converted to 0, while undefined is converted to NaN.

Calling Number() on NaN returns NaN itself.