parseInt()
Parses a string into an integer in a given base, stopping at the first invalid character.
parseInt(string)
parseInt(string, radix)- Added in JavaScript ECMAScript 1 (1997)
Parameters
| Parameter | Type | Description |
|---|---|---|
$string | string | A string starting with an integer. Leading whitespace is ignored. Other values are coerced to a string first. |
$radixoptional | number= undefined | An integer between 2 and 36 representing the base of the numeral. If 0 or omitted, the radix is inferred: 16 when the string begins with 0x or 0X, otherwise 10. |
Return value
numberAn integer parsed from the string, or NaN if the radix is outside 2–36 or the first non-whitespace character cannot be converted.
Changelog
- PHP ECMAScript 5Implementations may no longer treat a leading 0 as octal. parseInt("08") is 8 everywhere now, not 0.
parseInt() reads a numeral off the front of a string and ignores everything after it. That leniency is the whole point: parseInt("42px") gives you 42, where Number("42px") gives you NaN. Reach for it when the input is known to be a number with trailing junk — a CSS length, a version segment, an ID with a prefix you have already sliced off.
Always pass the radix
Without a radix the function guesses: a leading 0x means base 16, everything else means base 10. That guess is why parseInt("0x1f") returns 31 when you probably wanted 0. Writing parseInt(value, 10) removes the ambiguity and costs nothing.
What it will not do
It stops at the first character invalid for the base, so . and e end the parse: parseInt("1e3") is 1, and parseInt("3.9") is 3 — truncation toward zero, not rounding. It also stops at the n of a BigInt literal, silently losing precision. If the whole string must be a valid number, validate with Number() instead, which rejects trailing garbage and returns NaN.
Very large or very small values are parsed as doubles, so parseInt("1e21") is not what you might expect once the string form uses exponent notation.
Example
When to use it
- Pull the numeric part off a CSS value like "16px" before doing arithmetic.
- Convert a hexadecimal color component or a binary flag string to a number.
- Read an integer out of a URL segment or a user-typed field that may have stray spaces.
Frequently asked questions
Why does parseInt return NaN for my string?
Because the first non-whitespace character is not a valid digit for the radix in use. parseInt("px42") is NaN and parseInt("ff") is NaN unless you pass 16, since "f" is not a base-10 digit.
What is the difference between parseInt and Number?
parseInt stops at the first invalid character and returns what it read, while Number requires the whole string to be a valid numeric literal and returns NaN otherwise. Number("12px") is NaN; parseInt("12px", 10) is 12.
Do I really need to pass the radix?
Yes, in any code where the input is not fully under your control. Omitting it means a string beginning with 0x is read as hexadecimal, which turns "0x10" into 16 instead of 0.
More examples
The map(parseInt) trap
The first line prints [1, NaN, NaN, 3]: radix 0 falls back to 10, radix 1 is invalid, radix 2 rejects "3", and radix 3 reads "10" as 3. Both fixes print [1, 2, 3, 10].
const input = ["1", "2", "3", "10"];
// map passes (value, index), so index becomes the radix
console.log(input.map(parseInt));
// Fixes:
console.log(input.map((s) => parseInt(s, 10)));
console.log(input.map(Number));Same digits, different bases
"10" reads as 2, 8, 10, 16 and 36 depending on the base. The last line shows that an out-of-range digit at position zero yields NaN rather than a partial result.
const digits = "10";
for (const radix of [2, 8, 10, 16, 36]) {
console.log(radix, parseInt(digits, radix));
}
// Round-trip with Number.prototype.toString
console.log((255).toString(16)); // "ff"
console.log(parseInt("ff", 16)); // 255
console.log(parseInt("ff", 15)); // NaN — "f" is not a base-15 digitValidating instead of guessing
Shows the split in responsibilities: parseInt salvages a prefix, Number rejects any string that is not entirely numeric. Pick based on whether trailing characters are expected data or a bug.
function toInt(value) {
const n = parseInt(value, 10);
return Number.isNaN(n) ? null : n;
}
console.log(toInt(" 120 ")); // 120
console.log(toInt("12abc")); // 12 — parseInt is lenient
console.log(toInt("abc")); // null
console.log(Number("12abc")); // NaN — Number is strict
console.log(Number(" 120 ")); // 120
Discussion