Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarification regarding parseInt() in Javascript

parseInt(0.000004); //0 
parseInt(0.0000004);  //4 

why does the first parseInt() return 0, but if I increase the number of zeros after decimal it gives 4?

like image 498
Akash Goel Avatar asked Mar 21 '26 08:03

Akash Goel


1 Answers

It's partly because parseInt() expects a string for its argument and first converts anything else to a string.

console.log(0.000004.toString());
// "0.000004"

console.log(0.0000004.toString());
// "4e-7"

And, parseInt() doesn't recognize e-notation and, in the latter case, accepts just the "4" from the resulting string.

like image 198
Jonathan Lonowski Avatar answered Mar 22 '26 22:03

Jonathan Lonowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!