Akash Goel
Akash Goel

Reputation: 161

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?

Upvotes: 3

Views: 142

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123473

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.

Upvotes: 6

Related Questions