Weird Javascript: The Strange behaviour of parseInt()

Passionate Software Developer with a deep love for JavaScript, Web Development, and emerging technologies like Electric Vehicles, Blockchain, React.js, and Node.js. Enthusiastic about Open Charge Point Protocol and Open Charge Point Interface. Constantly curious and eager to explore new technologies, I thrive on continuous learning and enjoy coding while sipping a cup of tea ("chai"). Committed to open-source principles and believe in learning in public. Ready to embrace new challenges, expand my skillset, and make a positive impact in the world of software development. Let's code and learn together!
Welcome, folks, to another captivating article in my "Weird JavaScript" series! Today, we delve into the peculiar world of the parseInt() function of Javascript and its strange behaviour. JavaScript's parseInt() is a commonly used function for parsing strings into integers, but it holds some surprises. So without losing much time let's dive into exploring scenarios where it yields peculiar results.
Long back when I was working on an analytical app project, I encountered a strange behaviour of Javascript. I was trying to parse an integer from a decimal or floating-point number for the app requirements, I found a strange behaviour while using the native parseInt() function. When I tried to parse 0.3, 0.03 it went well as expected, but then I tried to parse 0.0000003, and surprisingly the result was not as expected!! While trying to do parseInt(0.0000003), it yielded the result 3. I again tried with a few other cases and the results yielded as mentioned below:
parseInt(0.3); // -> 0
parseInt(0.03); // -> 0
parseInt(0.003); // -> 0
parseInt(0.0003); // -> 0
parseInt(0.00003); // -> 0
parseInt(0.000003); // -> 0
parseInt(0.0000003); // -> 3
This is strange right ??? I was curious to find the reason behind it, then I went through the docs for parseInt() function and discovered something logical. In JavaScript, numbers are internally represented as 64-bit floating-point values using the IEEE 754 standard. Floating-point numbers have limited precision, which means they can't represent every possible decimal value with perfect accuracy. This limitation leads to potential precision issues when working with extremely small decimal numbers.
When we call parseInt() on a small decimal number like 0.0000003, it first converts the number to a string representation and then it simply takes the first character from that string !! In this case, the string representation is '3e-7'. You can try to verify this by passing 0.0000003 to the String() function. So after converting to string, Javascript will attempt to parse an integer from the beginning of the string until it encounters a non-digit character. In our case it is 3 since, it's a digit character and the next character is "e" which is a non-digit character, so Javascript stops here and ultimately yields 3 as the result of parseInt(0.0000003) . But this is not the case when we try to do parseInt(0.3) or parseInt(0.03) etc, because in these cases the string representation will be "0.3" and "0.03". Here Javascript will first parse 0 and will stop while parsing the next character because it is a decimal, and will finally yield the result 0.
It's important to note that parseInt() is primarily used to parse strings into integer values, and it may not always behave as expected with decimal numbers. To parse decimal numbers accurately, you can use methods like parseFloat() or convert the number explicitly using multiplication or other mathematical operations.
In conclusion, the behaviour you noticed with parseInt() is a result of the function's parsing rules and the limited precision of floating-point numbers. When working with very small decimal numbers, JavaScript's handling of number precision can produce unexpected results, so it's important to be aware of these restrictions to ensure accurate and reliable number parsing.
Okay, that's all for today's mind-blowing revelations. The strange behaviour of JavaScript's parseInt() has been exposed, but our investigation is far from over. Keep checking back for more fascinating excursions into the bizarre and fantastic world of JavaScript. Come back the next time as we continue to explore this extraordinary language's mysterious facets. Keep exploring and coding until then, and I'll see you in the upcoming thrilling adventures!


