A Silly JavaScript Interview Question
Here’s the question, which I found in a thread on Hacker News.
Explain this behavior:
['1','1','1'].map(parseInt) returns [1, NaN, 1]
['1','1','1'].map(n => parseInt(n)) returns [1, 1, 1]
Obviously this is a completely ridiculous question and I would seriously question why anyone would ask this during an interview. But that doesn’t mean we can’t have fun trying to find the answer.
I was stumped at first, especially when I tried playing with the input:
Ok, so let’s try to understand what the map
function actually does.
(Note that the arguments
object is not available in arrow functions) Ok, so this seems like it’s part of the answer - map
actually passes 3 arguments to the callback function: the current element, the current index, and the array map
was called upon.
So what about parseInt
?
The parseInt
function actually takes 2 parameters - the value to parse and the base (or radix) of the value. So our original code snippet is actually equivalent to the following:
I was surprised that parseInt('1',0)
actually returns the correct value, but according to the documentation passing 0 as the base/radix is equivalent to passing undefined
which means parseInt
will attempt to infer the base/radix, which it does correctly. parseInt('1',2)
just means parsing the number 1 in binary, which is perfectly valid.
I initially assumed that this was just some strange quirk of JavaScript, but it’s actually just an unfortunate interaction between 2 APIs. Nothing broken here. What a silly question.