Sitemap

JS parseInt()

2 min readJun 12, 2025

--

this writing is part of #AlfiLearnsToCompetitiveProgramming

Get Alysia Alfi’s stories in your inbox

Join Medium for free to get updates from this writer.

Before getting into Competitive Programming, I trapped into believe that integer is a set of positive number, thus parseInt() parsed string to number and removed everything preceding it including the sign.

parseInt("15"); // 15 ✅
parseInt("-15"); // 15 🚫
parseInt("-15"); // -15 ✅

Anatomy

Refer to MDN Docs, here’s the anatomy of parseInt()

parseInt(string, radix)
  • string: the string to be parsed.
  • radix (optional): the base of the numeral system (e.g., 2 for binary, 16 for hexadecimal).

Without the radix, parseInt() can produce unexpected results. For instance, a string with a leading zero, like "010", might be interpreted as an octal number in older JavaScript engines, resulting 8 instead of 10. While modern engines have standardized this behavior to a radix of 10, relying on this can make code less readable and potentially fragile across different environments.

parseInt("010"); // Potentially problematic
parseInt("010", 10); // Explicitly specifies decimal base for clarity

Behavior

Next, what actually happens under the hood so that a string is converted into number, is it as simple as removing the quote? So, here’s the step-by-step parsing behavior

  1. Removes leading whitespace from the string (' -15y0' → '-15y0')
  2. Reads an optional leading sign: + or - ('-')
  3. Parses numeric digits until it hits a character that’s invalid for the specified radix ('-15y0' → -15)
  4. Stops parsing at the first invalid character (-15 ignoring 0)
  5. If no valid numbers are found, returns NaN

Non-String Arguments

If a non-string value is passed, it is first converted to a string. This can lead to unexpected outcomes, especially with float numbers. Let’s take a look to this

parseInt(0.00005); // ➜ 5

Here’s what actually happens

  1. 0.00005 is a number
  2. JavaScript converts it to a string: "5e-5" (scientific notation)
  3. Resulting parseInt(“5e-7”)

Yes, this is a quirky behavior of parseInt() in JavaScript. It coerces non-strings to strings, and parsing a scientific notation can cause unexpected results.

Limitations with Large Numbers

A significant limitation of parseInt() is handling of numbers larger than the maximum safe integer in JavaScript (Number.MAX_SAFE_INTEGER, which is 253−1). Parsing a string representing a very large number can lead to a loss of precision without throwing an error. For applications requiring high precision with large integers use BigInt()

parseInt("9007199254740992"); // Returns 9007199254740992
parseInt("9007199254740993"); // Incorrect. returns 9007199254740992

BigInt("9007199254740993"); // Returns 9007199254740993n

Math.abs()

These all then arise a question, how to parse a string to return a positive or absolute number? Then Math.abs() comes to the rescue

Math.abs("-15"); // ➜ 15 ✅
Math.abs(-15); // ➜ 15 ✅

Conclusion

Integer is any number including 0, positive numbers, and negative numbers. Thus parseInt() preserves the minus sign in a number.

--

--