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

JavaScript · Function

parseInt

parseInt() converts a value to a string and parses an integer in a specified radix. It returns the parsed integer or NaN when the radix is invalid or no initial numeric characters can be parsed.

How it works

parseInt() converts its first argument to a string, ignores leading whitespace and an optional sign, then reads consecutive characters that are valid in the chosen radix. It stops at the first invalid character and returns the accumulated integer; if no valid integer can be read, or the radix is outside 2–36 after conversion, it returns NaN.

Syntax

parseInt(string)
parseInt(string, radix)

Parameters

NameTypeDescription
stringstringA value converted to a string and parsed from its beginning as an integer.
radixnumber, optionalAn integer base from 2 through 36. If omitted or zero, decimal is used except that a 0x or 0X prefix selects hexadecimal.

Returns

An integer parsed from the string, or NaN if the radix is outside 2–36 after conversion or the first non-whitespace character cannot be converted to a number.

Key points

  • The function parses text as an integer in a chosen base, such as decimal base 10 or hexadecimal base 16.
  • The radix is numerically coerced before range validation.
  • Whitespace before the number does not affect the result.
  • A leading plus or minus sign is supported.
  • Successful parsing returns an integer represented by the initial valid portion of the input.
  • The result is either a parsed integer or NaN when parsing cannot produce a valid value.

Common mistakes

  • Parsing stops silently at the first character invalid for the radix, so trailing text can be accepted rather than rejected.
  • Always provide a radix between 2 and 36; an invalid coerced radix produces NaN.

Example: Parse integers with explicit radices

const whole = parseInt("42px", 10);
const hexadecimal = parseInt("ff", 16);

console.log(whole); // 42
console.log(hexadecimal); // 255

Parses a decimal prefix and a hexadecimal string using explicit radices.

When to use it

Read integer text

Convert a decimal string such as "42" or a decimal prefix such as "42px" into an integer.

Parse non-decimal bases

Interpret binary, octal, hexadecimal, or another base from 2 through 36.

Change numeric bases

Convert a number represented in one radix before formatting it with Number.prototype.toString().

  • Number.parseInt()
  • parseFloat()
  • Number()
  • Number.prototype.toString()

Source: parseInt() - JavaScript | MDN. Checked 2026-10-15.