JavaScript parseInt() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Global function

Definition

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);
CallWhat 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

ParameterDescription
stringThe 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.
radixOptional. 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 ValueDescription
A whole numberThe 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.
NaNThe 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.

JavaScript
console.log("42 -> " + parseInt("42", 10));
console.log("42px -> " + parseInt("42px", 10));
console.log("  7 apples -> " + parseInt("  7 apples", 10));
console.log("3.99 -> " + parseInt("3.99", 10));
console.log("-15 -> " + parseInt("-15", 10));
console.log("abc -> " + parseInt("abc", 10));
console.log("px42 -> " + parseInt("px42", 10));
console.log("0.0000005 -> " + parseInt(0.0000005, 10));
Try It Yourself

How It Works

  1. Digits at the start are kept. "42px" gives 42. The letters after the number are ignored, and spaces at the start are skipped.
  2. Decimals are cut off. "3.99" gives 3, because the dot is not a digit. Nothing is rounded.
  3. No leading digit means NaN. "abc" and "px42" both give NaN, because the text must start with the number.
  4. 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));
Try It Yourself

How It Works

  1. Pick the base. parseInt("101", 2) reads the text as binary, so the answer is 5. parseInt("ff", 16) reads hexadecimal and gives 255.
  2. Letters are digits too. In base 36 the digits are 0-9 and a-z, so "z" is 35.
  3. The 0x prefix. With no radix, the text "0x1F" is read as hexadecimal and gives 31.
  4. It stops at a bad digit. In base 2 the digit 2 is not allowed, so "102" stops after "10" and gives 2.
  5. Go the other way. (5).toString(2) turns the number back into the binary text "101".

Example 3 — Avoid the map() Trap

Passing parseInt straight to map() looks neat, but it gives strange results. Here is why, and two safe fixes.

JavaScript
const parts = ["10", "10", "10"];

console.log("map(parseInt): " + parts.map(parseInt).join(", "));
console.log("with a radix: " + parts.map(text => parseInt(text, 10)).join(", "));
console.log("map(Number): " + parts.map(Number).join(", "));
Try It Yourself

How It Works

  1. 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.
  2. 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.
  3. Fix 1. Write an arrow function that passes only the text and the radix you want.
  4. Fix 2. If you want to read the whole text as a number, Number ignores the extra values and works well with map().

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.

ES1 Standard since
Google Chrome All versions · Desktop & Mobile
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Opera All versions
Full support
Samsung Internet All versions · Android
Full support
Android WebView All versions
Full support
Node.js All versions · Server runtime
Supported
Deno All versions · JavaScript runtime
Supported
Bun All versions · JavaScript runtime
Supported
Internet Explorer All 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.

Next: unescape()

Learn the last global function in this series: how the old unescape() turns percent-encoded text back into normal text.

unescape() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful