JavaScript parseInt()

parseInt() reads an integer from the beginning of a string. When the base matters, pass a radix from 2 to 36; using 10 makes decimal intent explicit.

console.log(parseInt("42", 10));   // 42
console.log(parseInt("101", 2));  // 5

parseInt() syntax and radix Top ↑

parseInt(string, radix)

The radix is an integer between 2 and 36. For ordinary decimal text, use parseInt(text, 10).

Basic decimal examples Top ↑

console.log(parseInt("2", 10));      // 2
console.log(parseInt("2.4", 10));    // 2
console.log(parseInt("-3.9", 10));   // -3
console.log(parseInt(" 50 ", 10));   // 50
console.log(parseInt("Hello", 10));  // NaN

parseInt() stops when an invalid digit is reached Top ↑

console.log(parseInt("5ab", 10)); // 5
console.log(parseInt("ab5", 10)); // NaN
console.log(parseInt("1e3", 10)); // 1, not 1000

Binary, octal and hexadecimal parsing Top ↑

console.log(parseInt("101", 2)); // 5
console.log(parseInt("15", 8));   // 13
console.log(parseInt("15", 16));  // 21
console.log(parseInt("5", 2));    // NaN

What happens when radix is omitted Top ↑

If no radix is supplied, a leading 0x or 0X is recognized as hexadecimal. Do not teach or depend on a leading zero as an octal signal. For clarity and portability, supply the radix when parsing known-base text.

console.log(parseInt("0xA"));       // 10
console.log(parseInt("011"));       // 11 in modern JavaScript parsing
console.log(parseInt("011", 8));    // 9

parseInt() vs Number() Top ↑

console.log(parseInt("42px", 10)); // 42
console.log(Number("42px"));       // NaN

Use Number() when the entire input must be numeric. Use parseInt() when reading an integer prefix is intentional.

parseInt() vs parseFloat() Top ↑

console.log(parseInt("19.95", 10)); // 19
console.log(parseFloat("19.95"));   // 19.95

See parseFloat() when fractional values are required.

Do not use parseInt() as a general truncation function Top ↑

parseInt() is a string parser. For a Number that you want to truncate toward zero, use Math.trunc(). Converting very large or very small Numbers to strings before parsing can also produce unexpected results because scientific notation may be used.

console.log(Math.trunc(-3.9)); // -3

Large integers and precision Top ↑

parseInt() returns a Number, so integer values beyond the safe-integer range may lose precision. For integer text that must remain exact at arbitrary size, consider BigInt() when appropriate.

Parsing an integer from a form field Top ↑

const raw = document.getElementById("age").value;
const age = parseInt(raw, 10);
if (Number.isNaN(age)) {
  console.log("Enter a valid integer");
}

See numeric validation, NaN, array toString(), and base conversion.

JavaScript Math Reference




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer