Why parseInt Returns Unexpected Results and How to Use It Correctly
This article explains how JavaScript's parseInt function works, its handling of radix and floating‑point numbers, why it produces surprising results with very small values, and recommends safer alternatives like Math.floor for extracting integer parts.
parseInt() is a built‑in JavaScript function that parses an integer from a numeric string. For example, parsing the string "100" returns 100.
const number = parseInt('100');
number; // 100As expected, "100" is parsed to 100.
parseInt(numericalString, radix) also accepts a second argument radix (2‑36) indicating the base of the string, defaulting to 10 (decimal).
Using parseInt() to parse a binary string:
const number = parseInt('100', 2);
number; // 4parseInt('100', 2) interprets "100" as a base‑2 number, returning 4 in decimal.
1. parseInt’s mysterious behavior
If the first argument to parseInt(numericalString) is not a string, JavaScript performs a ToString conversion before parsing.
parseInt(0.5); // => 0
parseInt(0.05); // => 0
parseInt(0.005); // => 0
parseInt(0.0005); // => 0
parseInt(0.00005); // => 0
parseInt(0.000005); // => 0Extracting the integer part of floats like 0.5 or 0.05 yields 0, as expected.
But what about extracting the integer part of 0.0000005? parseInt(0.0000005); // => 5 parseInt() parses the float 0.0000005 to 5, which is surprising.
Why does parseInt(0.0000005) behave this way?
2. Solving the parseInt mystery
Recall that if the first argument is not a string, it is converted to a string before parsing.
This is the first clue.
Now manually convert the float to a string:
String(0.5); // => '0.5'
String(0.05); // => '0.05'
String(0.005); // => '0.005'
String(0.0005); // => '0.0005'
String(0.00005); // => '0.00005'
String(0.000005); // => '0.000005'
String(0.0000005); // => '5e-7'Explicitly converting 0.0000005 to a string yields exponential notation.
The second clue: parsing this exponential notation extracts the integer 5:
parseInt(0.0000005); // => 5
// similar to
parseInt(5e-7); // => 5
// similar to
parseInt('5e-7'); // => 5parseInt('5e-7') reads the first digit "5" and skips the "e-7" part.
Mystery solved! Because parseInt always converts its first argument to a string, numbers smaller than 10⁻⁶ are represented in exponential form, and parseInt extracts the leading integer from that representation.
For safely extracting the integer part of a float, it is recommended to use Math.floor():
Math.floor(0.5); // => 0
Math.floor(0.05); // => 0
Math.floor(0.005); // => 0
Math.floor(0.0005); // => 0
Math.floor(0.00005); // => 0
Math.floor(0.000005); // => 0
Math.floor(0.0000005); // => 03. Conclusion
parseInt() parses numeric strings into integers, but when extracting the integer part of floating‑point numbers you must use it carefully to avoid unexpected results.
Now, can you explain why parseInt(999999999999999999999) equals 1? Share your answer in the comments.
Note: https://262.ecma-international.org/6.0/#sec-tostring
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JavaScript
Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
