Example 1 — Decimal String
Parse a simple integer string.
Number.parseInt("42"); // 42 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.
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.
Number only
integer / NaN
2 to 36
global parseInt
first bad char
Everywhere
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 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.
parseInt() MethodCall Number.parseInt(string) or Number.parseInt(string, radix). The engine:
"42px" still yields 42).NaN if no valid digit is found (or if radix is out of range).General form of the static method Number.parseInt:
Number.parseInt(string)
Number.parseInt(string, radix) 0, assume 10, unless the string starts with 0x/0X (then 16).An integer number, or NaN:
| Input | Returns |
|---|---|
Number.parseInt("42") | 42 |
Number.parseInt("42px") | 42 |
Number.parseInt("1010", 2) | 10 |
Number.parseInt("ff", 16) | 255 |
Number.parseInt("hello") | NaN |
NaN rather than a thrown error.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 | Goal | Code |
|---|---|
| Decimal integer from text | Number.parseInt(s, 10) |
| Binary | Number.parseInt(s, 2) |
| Hexadecimal | Number.parseInt(s, 16) |
| CSS size prefix | Number.parseInt("42px", 10) |
| Check failed parse | Number.isNaN(Number.parseInt(s)) |
| Strict whole-string number | Number(s) (not parseInt) |
Four facts to remember about Number.parseInt().
staticCall on Number, not instances
numberInteger or NaN
2–36Optional numeric base
parseIntSame as the global function
Number.parseInt() vs Number() vs global parseIntNumber.parseInt(s, r) | Number(s) | parseInt(s, r) | |
|---|---|---|---|
"42px" | 42 | NaN | 42 |
"15.9" | 15 (truncates) | 15.9 | 15 |
| Custom base | Yes (radix) | No | Yes (same) |
| Identity | Same function as global | Different | === Number.parseInt |
Examples follow MDN / parseInt patterns. Use View Output or Try It Yourself for each case.
Plain decimals and strings with trailing units.
Parse a simple integer string.
Number.parseInt("42"); // 42 With no radix (or radix 10), the digits are read as base 10. Prefer passing 10 explicitly in production code for clarity.
Useful for CSS-like values such as "42px".
Number.parseInt("42px"); // 42 Parsing stops at p. Compare with Number("42px"), which is NaN.
Other bases and failed parses.
Pass radix 2 to read bits.
Number.parseInt("1010", 2); // 10 Binary 1010 is 8 + 2 = 10 in decimal. Pair with (10).toString(2) for the reverse.
Pass radix 16 for hex digits a–f.
Number.parseInt("ff", 16); // 255 Hex ff is 15×16 + 15 = 255. An MDN-style helper might scale parsed hex like Number.parseInt("0xF", 16) * 100.
No leading digits means NaN.
Number.parseInt("hello"); // NaN Detect failure with Number.isNaN(): Number.isNaN(Number.parseInt("hello")) is true.
"16px".Number.parseInt(channel, 16).2.Number.parseInt() Reads a StringSkip leading whitespace; pick the radix.
Optional sign, then digits valid for the base.
First invalid character ends the parse.
Success yields a number; failure yields NaN.
Number.parseInt(s, radix) only.Number.parseInt === parseInt — same built-in function.10) to avoid surprises.Number.parseInt("15.9") is 15.0 case) yields NaN.=== NaN.Number.parseInt() is Baseline Widely available — part of ES2015 and supported in every modern browser and Node.js.
Safe for production everywhere. Same behavior as global parseInt, grouped under Number.
Bottom line: Use Number.parseInt with an explicit radix for readable parsing. Use Number() when the entire string must be a valid numeric literal.
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.
Number.parseInt(s, radix) on the constructor10, 2, or 16Number.isNaN(result) after parsing user inputtoString(radix) for round-trips(5).parseInt() — that is not a Number methodNumber())=== NaNNumber.parseInt()Static string-to-integer parsing with optional radix.
static
APInumber
Result2–36
Baseearly
SuffixparseInt
SameNumber.parseInt was added so number-related helpers live on Number, but it is literally the same function object as the global parseInt — Number.parseInt === parseInt is true in modern engines.
Return to the hub for instance and static Number tutorials.
8 people found this page helpful