parseInt is meant for strings so it converts the number there into a string. Once the numbers get small enough it starts representing it with scientific notation. So 0.0000001 converts into "1e-7" where it then starts to ignore the e-7 part because that’s not a valid int, so it is left with 1
parseInt() takes string as an input. From first character, it goes on till it hits a non-digit character, and then converts resulting string to an integer. JS is not strictly-typed, so, when I feed it a floating point number, it implicitly converts it to string. Things like 0.01 it converts like "0.01", no problem here, our first character is zero, and then there is a dot, that’s not a digit, so we parse "0" to integer and get our zero. But at some point it switches to scientific notation when converting to string, so, our 0.0000001 becomes "1E-7". Then we take one as our first character, stop at E as it’s not a digit and we get "1" parsed to one. Praise the loosly-typed hell.
Wait wtf is happening there?
parseInt is meant for strings so it converts the number there into a string. Once the numbers get small enough it starts representing it with scientific notation. So
0.0000001
converts into"1e-7"
where it then starts to ignore thee-7
part because that’s not a valid int, so it is left with1
https://javascript.plainenglish.io/why-parseint-0-0000001-0-8fe1aec15d8b
parseInt()
takes string as an input. From first character, it goes on till it hits a non-digit character, and then converts resulting string to an integer. JS is not strictly-typed, so, when I feed it a floating point number, it implicitly converts it to string. Things like0.01
it converts like"0.01"
, no problem here, our first character is zero, and then there is a dot, that’s not a digit, so we parse"0"
to integer and get our zero. But at some point it switches to scientific notation when converting to string, so, our0.0000001
becomes"1E-7"
. Then we take one as our first character, stop atE
as it’s not a digit and we get"1"
parsed to one. Praise the loosly-typed hell.