Convert a string to a number in JavaScript

An <input> value is always a string, even when the person typed digits. If that string is "42" and you add 1 with +, JavaScript joins the two strings and you get "421". To convert a string to a number in JavaScript, you choose a function from how much of the string is a number, and from how large the integer is.

<input id="age" value="42">
const age = document.querySelector("#age").value;

age + 1;          // "421"
Number(age) + 1;  // 43

document.querySelector("#age") finds the input in the page above, and .value is the string "42". The + between a string and a number converts the number to a string and concatenates, so "42" + 1 is "421". Number(age) reads the whole string as a number, and 42 + 1 is 43.

Number()

Number() converts the entire string. If any character does not belong in the number, the result is NaN. It does not keep a leading number and drop the rest.

Number("42");     // 42
Number("3.14");   // 3.14
Number("  42 ");  // 42
Number("");       // 0
Number(" ");      // 0
Number("42px");   // NaN
Number("0x10");   // 16

A blank string and a string of only spaces both become 0. Spaces around a number are ignored. "42px" is NaN because p is not part of a numeric literal. "0x10" is 16, because Number() accepts a 0x hex prefix, the same way a numeric literal does. A string a visitor typed can start with 0x by accident, and Number() will not treat that prefix as decimal.

Unary plus

For a string, the unary plus does the same conversion as Number(). It turns the value into a number and does not do a second operation on it.

+"42";    // 42
+"3.14";  // 3.14
+"";      // 0
+"42px";  // NaN
+"0x10";  // 16

The unary plus does not accept a BigInt. +1n throws a TypeError. Number(1n) is the call that converts a BigInt to a number, and that conversion can drop precision when the integer is larger than Number can store exactly.

parseInt()

parseInt() reads an integer from the start of the string and stops at the first character that is not a digit in the radix you pass. The rest of the string is ignored.

parseInt("42px", 10);  // 42
parseInt("3.14", 10);  // 3
parseInt("", 10);      // NaN
parseInt("ff", 16);    // 255
parseInt("10", 2);     // 2
parseInt("1e3", 10);   // 1

The second argument is the radix, from 2 to 36. 10 is decimal, so "42px" becomes 42 and the px is ignored. The decimal point is not a digit, so parseInt("3.14", 10) is 3. 16 is hexadecimal, so "ff" is 255. 2 is binary, so "10" is 2. An empty string has no digit, so the result is NaN.

"1e3" means 1000 to Number() and to parseFloat(), because e marks an exponent. To parseInt() with radix 10, e is not a digit, so parseInt("1e3", 10) is 1. That is also why parseInt is a poor way to chop the fraction off a number you already have. Very large and very small numbers turn into strings like "1.5e+22" before parseInt sees them, and it stops at the e or the dot. Math.trunc() removes the fraction from a number.

If the radix is not 0 and it sits outside 2 through 36, parseInt() returns NaN. Passing 0, or leaving the radix out, does not mean decimal in every case. After optional spaces and a sign, a string that starts with 0x or 0X is read as hexadecimal. Every other string is read as decimal, and a leading 0 is not octal. parseInt("08") is 8. Pass 10 when you want decimal, so a 0x prefix cannot switch the base.

parseFloat()

parseFloat() reads a decimal from the start of the string. It keeps a fractional part and an exponent. It takes no radix.

parseFloat("3.14");    // 3.14
parseFloat("3.14px");  // 3.14
parseFloat("314e-2");  // 3.14
parseFloat("");        // NaN
parseFloat(" ");       // NaN

"3.14px" stops at p and returns 3.14. "314e-2" is 3.14. An empty string, or a string of only spaces, is NaN. That is the useful difference from Number() and the unary plus, which turn those strings into 0. parseFloat() also does not treat 0x, 0b, or 0o as a base prefix.

NaN

NaN is the failure value from Number(), parseInt(), and parseFloat(). It is not equal to itself, so a check written as amount === NaN is always false, including when amount is NaN.

const amount = Number("42px");

amount === NaN;        // false
Number.isNaN(amount);  // true
Number.isNaN("foo");   // false
isNaN("foo");          // true

Number("42px") is NaN, and Number.isNaN returns true only for that NaN value. It does not convert the argument first, so Number.isNaN("foo") is false. The global isNaN("foo") converts "foo" to a number, gets NaN, and returns true. After you have already called Number, parseInt, or parseFloat, Number.isNaN tells you whether that call failed.

A blank field is the case where the functions disagree. Number("") is 0, which will quietly add and multiply. parseFloat("") is NaN, which you can reject. If a missing value should not become zero, test the string before calling Number(), or call parseFloat() and check the result with Number.isNaN.

BigInt()

Integers past Number.MAX_SAFE_INTEGER (9007199254740991) are not all distinct as a Number. Number("9007199254740993") is 9007199254740992. BigInt() parses an integer string without that rounding.

Number("9007199254740993");  // 9007199254740992
BigInt("9007199254740993");  // 9007199254740993n

BigInt("3.14") throws a SyntaxError. The argument has to be an integer, with no decimal point. A BigInt and a Number cannot be mixed in arithmetic. 1n + 1 throws a TypeError. Convert both sides to the same type before you add them.

Use Number() or the unary plus when the whole string should be a number and a blank string should count as 0. Use parseInt() with a radix, or parseFloat(), when you want a numeric prefix and you want a blank string to come back as NaN. Use BigInt() when the value is an integer that may be larger than Number.MAX_SAFE_INTEGER.

If this article was helpful, tweet it!