Puzzle time: Yes, this is not good code, but do you know why this quirk occurs? Answer will be posted (or retweeted) later :-) Clue: An intermediate step occurs in the transformation of the supplied number.

Apr 17, 2018 · 4:53 PM UTC

String(0.000001) = "0.000001" => parseInt: 0 String(0.0000001) = "1e-7" => parseInt: 1
1
36
5
128
Had you used #TypeScript, it wouldn't have even compiled. parseInt isn't quirky. Implicit coercion is. parseInt doesn't even take number as the argument! It takes string!
2
7
1
33
parseInt(String(0.0000001))==parseInt(0.0000001) String(0.0000001)=>"1e-7" so parseInt(0.0000001) == 1 but parseInt(String(0.000001))==parseInt(0.000001) String(0.000001)=>"0.000001" so parseInt(0.000001) == 0 if we use parseInt(Num(0.0000001)) == 0
5
parseInt(val) 1. Converts val to string. Numbers get converted to exponential notation if they have too many digits (like 0.0000001 has). 2. Strips anything after, and including, the first non-digit 3. Converts to integer 0.000001→"0.000001"→"0"→0 0.0000001→"1e-7"→"1"→1
1
5
"parseInt" function convert value to the string. So String(0.0000001) will return 1e-7, and parseInt('1e-7') will return 1.
2
4
Interesting! Just sent in a pull request with this quirk to #wtfjs github.com/denysdovhan/wtfjs… Thanks @JavaScriptDaily
1
2
The number is first converted to a string — and unfortunately 0.000001 is converted to exponent format (1e-7) which parseInt will read and take the ‘1’.
1
2
Because it first use toString method. And it can’t parse numbers less than 1e-6 and bigger than 1e21.
1
2
not that it makes a difference in this case, but you should always practice safe `parseInt` by using a radix...
1
the second number will be converted to the exponential notation. parseInt takes a string as an arg so it is basically parseInt(‚1e-1000‘) or something like that. Everything after the e will be cut off.
1
parseInt takes a string as first parameter. 0.0000001 is transformed to 1e-7... so parseInt("1e-7") and that's why the result is 1