Sub topic 7
Turning Strings into Numbers: Number(), parseInt, parseFloat, and Unary Plus
This chapter assumes you can write basic JavaScript, run code in a browser console or Node.js, and know what typeof and === do. It also assumes you have read the earlier chapter on NaN, because most failed conversions produce it.
Input from forms, URLs, files, and APIs usually arrives as strings. JavaScript has four common ways to turn those strings into numbers. They follow two different rules.
Two Conversion Models
- Whole-string conversion:
Number(value)and unary plus (+value) read the entire string. Leading and trailing whitespace is ignored. Anything else that is not part of a valid number makes the resultNaN. - Prefix parsing:
parseIntandparseFloatread from the start of the string and stop at the first character they cannot use. They return whatever they have read so far, orNaNif no digits came first.
console.log(Number("42")); // 42
console.log(Number("42px")); // NaN
console.log(+" 12 "); // 12
console.log(parseInt("42px", 10)); // 42
console.log(parseFloat("3.14abc")); // 3.14
console.log(parseInt("px42", 10)); // NaNUnary plus behaves exactly like Number(). It is shorter to type, while Number() is easier to spot when reading code.
Edge Cases of Number()
Number() accepts more than decimal strings, and some of its results surprise people:
console.log(Number("")); // 0
console.log(Number(" ")); // 0
console.log(Number(null)); // 0
console.log(Number(undefined)); // NaN
console.log(Number(true)); // 1
console.log(Number("0x1F")); // 31
console.log(Number("1e3")); // 1000
console.log(Number("1_000")); // NaN
console.log(Number("12 34")); // NaNNumeric separators such as 1_000 are allowed only in source code literals. They are not accepted inside strings.
parseInt and the Radix
parseInt(string, radix) returns an integer. The radix is the number base, from 2 to 36. Always pass it. Without a radix, a string starting with 0x is read as hexadecimal, and the behavior is harder to see in the code.
console.log(parseInt("1F", 16)); // 31
console.log(parseInt("0x1F")); // 31 (radix inferred)
console.log(parseInt("0x1F", 10)); // 0 (stops at "x")
console.log(parseInt("101", 2)); // 5
console.log(parseInt("1e3", 10)); // 1 (stops at "e")
console.log(parseFloat("1e3")); // 1000parseInt converts its argument to a string first. That causes a classic bug:
console.log(parseInt(0.0000005, 10)); // 5, because String(0.0000005) is "5e-7"To drop the fraction from a value that is already a number, use Math.trunc. Do not use parseInt for that.
parseFloat always reads base 10. It understands decimals, exponents, and "Infinity", but it does not understand hex prefixes: parseFloat("0x1F") returns 0.
Choosing a Method
| Input | `Number()` | `parseInt(s, 10)` | `parseFloat()` |
|---|---|---|---|
| `"42px"` | `NaN` | `42` | `42` |
| `""` | `0` | `NaN` | `NaN` |
| `"3.9"` | `3.9` | `3` | `3.9` |
| `"1e3"` | `1000` | `1` | `1000` |
| `"0x10"` | `16` | `0` | `0` |
For validating user input, Number() is usually the safer choice, because it rejects trailing text. Combine it with an empty-string check:
function toNumberStrict(input) {
if (input.trim() === "") return NaN;
return Number(input);
}
console.log(toNumberStrict("19.99")); // 19.99
console.log(toNumberStrict("")); // NaN
console.log(toNumberStrict("19abc")); // NaNExercise
A CSS value arrives as the string "12.5rem". What do Number("12.5rem"), parseInt("12.5rem", 10), and parseFloat("12.5rem") return, and which one fits if you need the size as a number?
Show answer
Number returns NaN because the whole string is not numeric. parseInt returns 12 because it stops at the decimal point. parseFloat returns 12.5, which is the correct size, so use parseFloat.
Exercise
Why does ["1", "2", "3"].map(parseInt) return [1, NaN, NaN]?
Show answer
map calls its callback with (element, index), so the index is passed as the radix. parseInt("1", 0) treats radix 0 as unspecified and returns 1. parseInt("2", 1) returns NaN because 1 is not a valid radix. parseInt("3", 2) returns NaN because 3 is not a binary digit. Use .map(Number) or .map(s => parseInt(s, 10)) instead.