parseInt() reads text from left to right and gives you back a whole number. It stops at the first character that cannot be part of the number, so "42px" becomes 42.
If the text does not start with a digit, you get NaN ("not a number"). The second argument, the radix, tells JavaScript which number base to read: 10 for normal numbers, 2 for binary, 16 for hexadecimal.
Remember
parseInt(string, radix);
string -> the text to read (spaces at the start are skipped)
radix -> number base from 2 to 36 (use 10 for normal numbers)
returns -> a whole number, or NaN
parseInt stops at the first character that does not fit.
Syntax
Call parseInt() by name. Give it the text, and always add the radix so the result is predictable.
JavaScript
parseInt(string);
parseInt(string, radix);
Call
What it does
parseInt("42px", 10)
Reads 42 and stops at "p". Result: 42.
parseInt("101", 2)
Reads 101 as a binary number. Result: 5.
parseInt("ff", 16)
Reads ff as a hexadecimal number. Result: 255.
parseInt("hello", 10)
The text does not start with a digit. Result: NaN.
Built into JavaScript (ES1). It is a global function, so you call it by name with no import. Number.parseInt is the very same function. See also: parseFloat(), isNaN() and isFinite().
Library declarations
JavaScript
// Global function (ES1)
parseInt(string)
parseInt(string, radix)
// returns a number (a whole number) or NaN
// Number.parseInt === parseInt // true
Parameters
Parameter
Description
string
The text to read. A value that is not text is converted to text first. Spaces, tabs and line breaks at the start are skipped. An optional + or - sign is allowed.
radix
Optional. A whole number from 2 to 36 that sets the number base. If you leave it out (or pass 0), JavaScript uses base 10, or base 16 when the text starts with 0x. A radix outside 2 to 36 gives NaN.
Return Value
Return Value
Description
A whole number
The digits found at the start of the text, read in the chosen base. Anything after the first character that does not fit is ignored. A decimal part is cut off, not rounded.
NaN
The first character (after any spaces and sign) cannot be a digit in that base, or the text is empty.
JavaScript Examples
Example 1 — Read a Whole Number from Text
See what parseInt() keeps, what it ignores, and when it gives up.
42 -> 42
42px -> 42
7 apples -> 7
3.99 -> 3
-15 -> -15
abc -> NaN
px42 -> NaN
0.0000005 -> 5
How It Works
Digits at the start are kept."42px" gives 42. The letters after the number are ignored, and spaces at the start are skipped.
Decimals are cut off."3.99" gives 3, because the dot is not a digit. Nothing is rounded.
No leading digit means NaN."abc" and "px42" both give NaN, because the text must start with the number.
Careful with tiny numbers.0.0000005 is turned into the text "5e-7" first, so only the 5 is read.
Example 2 — Read Numbers in Other Bases
The radix chooses the number base. Binary, octal and hexadecimal all work with the same function.
JavaScript
console.log("101 in base 2: " + parseInt("101", 2));
console.log("17 in base 8: " + parseInt("17", 8));
console.log("ff in base 16: " + parseInt("ff", 16));
console.log("z in base 36: " + parseInt("z", 36));
console.log("0x1F with no radix: " + parseInt("0x1F"));
console.log("102 in base 2: " + parseInt("102", 2));
console.log("back to base 2: " + (5).toString(2));
map(parseInt): 10, NaN, 2
with a radix: 10, 10, 10
map(Number): 10, 10, 10
How It Works
map() sends three values. It calls the function with the item, its index and the whole array. parseInt uses the second value as the radix.
So the radix changes. The three calls are parseInt("10", 0), parseInt("10", 1) and parseInt("10", 2). They give 10, NaN (base 1 does not exist) and 2.
Fix 1. Write an arrow function that passes only the text and the radix you want.
Fix 2. If you want to read the whole text as a number, Number ignores the extra values and works well with map().
Compatibility
Browser & Runtime Support
parseInt() is one of the oldest functions in JavaScript. Every browser and every Node.js version has it.
✓ ES1 · Works everywhere
parseInt()
Supported in all browsers, including old ones, and in Node.js, Deno and Bun.
ES1Standard since
Google ChromeAll versions · Desktop & Mobile
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
OperaAll versions
Full support
Samsung InternetAll versions · Android
Full support
Android WebViewAll versions
Full support
Node.jsAll versions · Server runtime
Supported
DenoAll versions · JavaScript runtime
Supported
BunAll versions · JavaScript runtime
Supported
Internet ExplorerAll versions
Full support
parseInt()Excellent
Bottom line: Safe to use anywhere. Very old engines treated text that starts with 0 as octal when no radix was given, which is one more reason to always pass the radix.
Frequently Asked Questions
It reads a whole number from the start of a string and returns it as a number. It stops at the first character that does not fit, so parseInt("42px", 10) returns 42. If the string does not start with a digit, the result is NaN.
The radix sets the number base. If you leave it out, the base depends on the text: a string that starts with 0x is read as hexadecimal, otherwise as base 10. Writing parseInt(text, 10) makes the result the same every time and shows other developers what you meant.
parseInt() reads as much as it can from the start and ignores the rest, so parseInt("42px", 10) is 42. Number() is strict: Number("42px") is NaN. Number() also keeps decimals, while parseInt() cuts them off.
map() passes the item, the index and the array. parseInt() takes the index as its radix, so the calls become parseInt("1", 0), parseInt("2", 1) and parseInt("3", 2). Use map(text => parseInt(text, 10)) or map(Number) instead.
Check the result with Number.isNaN(). For example, const n = parseInt(text, 10); if (Number.isNaN(n)) { ... } handles text that is not a number. Remember that parseInt("12abc", 10) still returns 12, so use Number() if you need the whole text to be a number.