Skip to content

parseInt()

The global parseInt() function reads an integer from the start of a string in a given radix and returns it as a number, or NaN when no integer starts the string. Parsing stops at the first character that isn’t a valid digit, so trailing text is ignored:

Language: JavaScript
> parseInt("42px")
42
> parseInt("ff", 16)
255

> parseInt("px42")
NaN

parseInt() is an ECMAScript built-in. The static method Number.parseInt() is the same function object, so the two always give the same result.

Syntax

The parseInt() function takes a string and an optional radix:

Language: JavaScript Syntax
parseInt(<string>[, <radix>])
Parameter Description
string The value to parse. A non-string value is converted to a string first, and leading whitespace is ignored
radix An optional integer from 2 to 36 giving the base of the digits. When it’s omitted or 0, the base is 16 for strings starting with 0x or 0X and 10 otherwise

The function isn’t a constructor, so calling it with new throws a TypeError. With a radix of 16, an optional 0x or 0X prefix is skipped. No other prefix is recognized, so 0b, 0o, and a leading 0 are read as ordinary digits.

Return Value

The result is a number with no fractional part. A leading + or - sets the sign, and "-0" returns -0.

The result is NaN when a nonzero radix is outside the range from 2 to 36, or when the string has no valid digit after the whitespace and sign. Trailing characters that aren’t digits in the radix, such as . or px, are ignored and never cause an error.

To see how the scan works, pick a string and sweep the radix to watch which characters parseInt() reads and where it stops:

Interactive diagram — enable JavaScript to view.

Examples

A layout script reads a CSS width such as "250px" and needs the number to do arithmetic with. Parsing stops at p, so the unit is ignored:

Language: JavaScript
> const width = "250px";
undefined

> parseInt(width, 10) + 20
270

A color picker stores colors as hex strings like "#1e90ff". Each two-character pair is a channel, and a radix of 16 reads it as hexadecimal:

Language: JavaScript
> const color = "#1e90ff";
undefined

> parseInt(color.slice(1, 3), 16)
30
> parseInt(color.slice(3, 5), 16)
144
> parseInt(color.slice(5, 7), 16)
255

A configuration file stores feature flags as a binary string. A radix of 2 turns it into a number for bitwise checks, and a digit that’s invalid in the radix ends the parse:

Language: JavaScript
> parseInt("1011", 2)
11
> parseInt("1021", 2)
2

Common Gotchas

Passing parseInt straight to .map() sends each element’s index as the radix. Index 1 is an invalid radix, and index 2 reads "10" as binary:

Language: JavaScript
> ["10", "10", "10"].map(parseInt)
[ 10, NaN, 2 ]
> ["10", "10", "10"].map((text) => parseInt(text, 10))
[ 10, 10, 10 ]

Very small or very large numbers convert to strings in exponential notation, such as "5e-7". Parsing stops at the e, so the result is only the first digit:

Language: JavaScript
> parseInt(0.0000005)
5
> parseInt(1e21)
1

To drop the fractional part of a number, Math.trunc() works on the number directly, without the string round trip.

A developer pulls a Math.floor() lever on a counting machine with a coffee sack on its scale, a big dial, a % gauge and a toFixed(2) receipt printer.

Course

JavaScript Numbers for Pythonistas

Port a coffee roastery's batch planner from Python to JavaScript, and learn to divide, round, read numbers from text, and format them with JavaScript's one number type.

basics js py

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 13, 2026