Skip to content

Instantly share code, notes, and snippets.

@kurtbrose
Created July 9, 2012 19:38
Show Gist options
  • Save kurtbrose/3078448 to your computer and use it in GitHub Desktop.
Save kurtbrose/3078448 to your computer and use it in GitHub Desktop.
javascript parseInt(Infinity) lolwut
1/0
Infinity
parseInt(1/0, 5)
NaN
parseInt(1/0, 19)
18
parseInt(1/0, 20)
18
parseInt(1/0, 45)
NaN
parseInt(1/0, 22)
18
What an evil interview question to ask WTF is going on here...
Here's wtf is going on:
parseInt is coercing Infinity to the string "Infinity;
then, apparently JS will extend hex notation, so the digits of base 19 are 0123456789ABCEFGHI
parseInt parses as much as it can (1 digit, 'I') whose value is 18.
Here by passing some different strings, the behavior becomes clear:
parseInt("H", 19)
17
parseInt("G", 19)
16
(In a way you are asking for it: what else is the function supposed to do when asked to parse integers of a base greater than 16?
Well, probably it should error out...)
Copy link

ghost commented Nov 23, 2016

"Evil" is correct!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment