JavaScript Number parseInt() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Static method

What You’ll Learn

Number.parseInt() is a static method on Number (same API as MDN Number.parseInt). Call Number.parseInt(string, radix) — not on a number instance. It turns text into an integer, optionally in base 2–36. This tutorial covers syntax, radix, junk suffixes, five examples, and try-it labs.

01

Call on

Number only

02

Returns

integer / NaN

03

Radix

2 to 36

04

Same as

global parseInt

05

Stops at

first bad char

06

Baseline

Everywhere

Introduction

Form fields, CSS values, and URLs often store numbers as text — "42", "42px", or even "ff" in hex. Number.parseInt() reads the leading integer from that text.

It is the same function as the global parseInt ((Number.parseInt === parseInt). The Number. form keeps number helpers together on one object.

💡
Static Method

Static methods belong to Number itself. Always write Number.parseInt(s, radix). There is no (5).parseInt() on Number.prototype.

This page is part of JavaScript Number Methods. Related tools include toString(radix) for the reverse direction, and Number.isNaN() to check failed parses.

Understanding the parseInt() Method

Call Number.parseInt(string) or Number.parseInt(string, radix). The engine:

  • Coerces the first argument to a string and skips leading whitespace.
  • Reads a sign and digits that are valid for the chosen radix.
  • Stops at the first invalid character (so "42px" still yields 42).
  • Returns NaN if no valid digit is found (or if radix is out of range).

📝 Syntax

General form of the static method Number.parseInt:

JavaScript
Number.parseInt(string)
Number.parseInt(string, radix)

Parameters

  • string (any) — value to parse (coerced to string). Leading whitespace is ignored.
  • radix (number, optional) — integer from 2 to 36. If omitted or 0, assume 10, unless the string starts with 0x/0X (then 16).

Return value

An integer number, or NaN:

InputReturns
Number.parseInt("42")42
Number.parseInt("42px")42
Number.parseInt("1010", 2)10
Number.parseInt("ff", 16)255
Number.parseInt("hello")NaN

Exceptions

  • None for normal calls — bad input yields NaN rather than a thrown error.

Common patterns

JavaScript
Number.parseInt("42");           // 42
Number.parseInt("42px");         // 42
Number.parseInt("1010", 2);      // 10
Number.parseInt("ff", 16);       // 255
Number.parseInt("hello");        // NaN
Number.parseInt === parseInt;    // true

⚡ Quick Reference

GoalCode
Decimal integer from textNumber.parseInt(s, 10)
BinaryNumber.parseInt(s, 2)
HexadecimalNumber.parseInt(s, 16)
CSS size prefixNumber.parseInt("42px", 10)
Check failed parseNumber.isNaN(Number.parseInt(s))
Strict whole-string numberNumber(s) (not parseInt)

🔍 At a Glance

Four facts to remember about Number.parseInt().

Kind
static

Call on Number, not instances

Returns
number

Integer or NaN

Radix
2–36

Optional numeric base

Alias
parseInt

Same as the global function

📋 Number.parseInt() vs Number() vs global parseInt

Number.parseInt(s, r)Number(s)parseInt(s, r)
"42px"42NaN42
"15.9"15 (truncates)15.915
Custom baseYes (radix)NoYes (same)
IdentitySame function as globalDifferent=== Number.parseInt

Examples Gallery

Examples follow MDN / parseInt patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

Plain decimals and strings with trailing units.

Example 1 — Decimal String

Parse a simple integer string.

JavaScript
Number.parseInt("42");  // 42
Try It Yourself

How It Works

With no radix (or radix 10), the digits are read as base 10. Prefer passing 10 explicitly in production code for clarity.

Example 2 — Trailing Junk

Useful for CSS-like values such as "42px".

JavaScript
Number.parseInt("42px");  // 42
Try It Yourself

How It Works

Parsing stops at p. Compare with Number("42px"), which is NaN.

📈 Practical Patterns

Other bases and failed parses.

Example 3 — Binary Radix

Pass radix 2 to read bits.

JavaScript
Number.parseInt("1010", 2);  // 10
Try It Yourself

How It Works

Binary 1010 is 8 + 2 = 10 in decimal. Pair with (10).toString(2) for the reverse.

Example 4 — Hexadecimal Radix

Pass radix 16 for hex digits af.

JavaScript
Number.parseInt("ff", 16);  // 255
Try It Yourself

How It Works

Hex ff is 15×16 + 15 = 255. An MDN-style helper might scale parsed hex like Number.parseInt("0xF", 16) * 100.

Example 5 — Failed Parse

No leading digits means NaN.

JavaScript
Number.parseInt("hello");  // NaN
Try It Yourself

How It Works

Detect failure with Number.isNaN(): Number.isNaN(Number.parseInt("hello")) is true.

🚀 Common Use Cases

  • Form inputs — turn typed text into integers for counts and ages.
  • CSS / DOM sizes — pull the number out of "16px".
  • Hex colors / bytesNumber.parseInt(channel, 16).
  • Binary flags — parse bit strings with radix 2.
  • URL / query values — convert page numbers from strings.
  • Not for floats — use Number.parseFloat when decimals matter.

🧠 How Number.parseInt() Reads a String

1

Coerce to string

Skip leading whitespace; pick the radix.

Input
2

Read digits

Optional sign, then digits valid for the base.

Scan
3

Stop early

First invalid character ends the parse.

Boundary
4

Return integer or NaN

Success yields a number; failure yields NaN.

📝 Notes

  • This is a static method — use Number.parseInt(s, radix) only.
  • Number.parseInt === parseInt — same built-in function.
  • Prefer an explicit radix (often 10) to avoid surprises.
  • Truncates toward zero for decimal points: Number.parseInt("15.9") is 15.
  • Radix outside 2–36 (except the special 0 case) yields NaN.
  • Check failures with Number.isNaN(), not === NaN.

Browser & Runtime Support

Number.parseInt() is Baseline Widely available — part of ES2015 and supported in every modern browser and Node.js.

Baseline · Widely available

Number.parseInt()

Safe for production everywhere. Same behavior as global parseInt, grouped under Number.

100% Universal support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Number.parseInt() Excellent

Bottom line: Use Number.parseInt with an explicit radix for readable parsing. Use Number() when the entire string must be a valid numeric literal.

Conclusion

Number.parseInt() is the static, modular way to turn text into integers — with optional bases for binary and hex, and tolerant parsing of suffixes like px.

Continue with toString() for base conversion the other way, Number.isNaN() for failed parses, or return to the Number methods hub.

💡 Best Practices

✅ Do

  • Call Number.parseInt(s, radix) on the constructor
  • Pass an explicit radix — usually 10, 2, or 16
  • Check Number.isNaN(result) after parsing user input
  • Use it for CSS sizes and hex color channels
  • Pair with toString(radix) for round-trips

❌ Don’t

  • Write (5).parseInt() — that is not a Number method
  • Omit radix when the input base matters
  • Expect floats — decimals are truncated
  • Use it when the whole string must be a pure number (Number())
  • Compare the result with === NaN

Key Takeaways

Knowledge Unlocked

Five things to remember about Number.parseInt()

Static string-to-integer parsing with optional radix.

5
Core concepts
📝 02

Returns

number

Result
🔢 03

Radix

2–36

Base
px 04

Stops

early

Suffix
05

Alias

parseInt

Same

❓ Frequently Asked Questions

Number.parseInt(string, radix) reads a string and returns an integer. Optional radix sets the base from 2 to 36. Example: Number.parseInt("42") is 42; Number.parseInt("ff", 16) is 255.
Yes. Call it on Number itself: Number.parseInt(s, radix). It is the same function as the global parseInt — Number.parseInt === parseInt is true.
radix is an optional integer from 2 to 36 for the numeric base. Use 2 for binary, 10 for decimal, 16 for hexadecimal. If omitted or 0, base 10 is assumed unless the string starts with 0x / 0X (then hex).
Parsing stops at the first character that is not valid for the chosen radix. Number.parseInt("42px") returns 42. If nothing valid is found at the start, the result is NaN.
Number("42px") is NaN because the whole string must be a valid number. Number.parseInt("42px") is 42 because it reads a leading integer prefix.
It is Baseline Widely available (ES2015) and supported in every modern browser and Node.js.
Did you know?

Number.parseInt was added so number-related helpers live on Number, but it is literally the same function object as the global parseIntNumber.parseInt === parseInt is true in modern engines.

More Number Methods

Return to the hub for instance and static Number tutorials.

Number methods hub →

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.

8 people found this page helpful