Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

parseInt radix

parseInt(str, radix) reads an integer prefix using the specified base, from 2 through 36. It stops at the first character that is not valid in that base and returns the digits read so far.

How it works

parseInt() converts its first argument to a string, reads an optional sign and leading whitespace, then consumes as many valid digits as possible in the chosen radix. It returns the resulting integer, or NaN if no valid digit can be read.

Syntax

parseInt(str, radix)

Parameters

NameTypeDescription
strstring or value convertible to stringThe value to convert; it is interpreted as a string.
radixnumberThe numeral-system base used for parsing, from 2 through 36.

Returns

An integer parsed from the string; the function returns NaN when no valid number can be read.

Key points

  • parseInt returns an integer, unlike parseFloat, which returns a floating-point value.
  • The second argument chooses the numeral system, allowing binary, hexadecimal, and other bases.
  • The supported radix range is 2 through 36, inclusive.
  • Parsing stops at the first character that cannot be interpreted in the selected radix.

Common mistakes

  • Parsing is prefix-based: trailing characters such as "px" do not make an otherwise valid numeric prefix invalid.
  • Always provide the radix when the intended numeral system matters; omitting it invokes default handling rather than explicitly documenting the base.
  • A malformed suffix can be silently ignored, so parseInt("12.3", 10) returns 12 rather than rejecting the input.
  • The radix must be between 2 and 36; values outside that range produce NaN.

Example: Parsing integers with explicit radices

const decimal = parseInt("101", 2);
const hexadecimal = parseInt("ff", 16);
const partial = parseInt("42px", 10);

console.log(decimal); // 5
console.log(hexadecimal); // 255
console.log(partial); // 42

The radix explicitly selects the numeral system, while parsing stops when a character is not valid in that radix.

When to use it

Parse non-decimal numbers

Convert binary, hexadecimal, or another-base text into a JavaScript integer.

Read numeric prefixes

Extract an integer prefix from text such as "42px".

Interpret numeric strings

Convert user or file input into an integer after choosing the intended numeral system.

  • parseFloat
  • Number.toString

Source: Numbers. Checked 2026-10-15.