DEV Community

Discussion on: Explain this Javascript expression just like I'm five

Collapse
 
aurelkurtula profile image
aurel kurtula • Edited

parseInt() takes to arguments, a string and a radix. I usually pass just the string I want converting as a number, you are passing both the string and the radix!

map gives us the value and the index (and the actual array)

["10", "10", "10", "10"].map( (value,index) => ... )

By just doing .map(parseInt) parseInt is taking both the value and the index.

Basically the second iteration is parseInt('10',1) and returns NaN.

Finally, to test that that's what happens, the following would return "0: 10" "1: 10" "2: 10" "3: 10"

function myFunc(value, i){
  console.log(`${i}: ${value}`)
}
["10", "10", "10", "10"].map(myFunc)