8

['1', '7', '11'].map(parseInt)
// and this returns...
// drumroll please
[1, NaN, 3]

WAT

Comments
  • 15
    That's because parseInt takes two arguments.
    It expects the 2nd argument to be the radix, but map passes the index instead.
    Or in short, RTFM!!!
  • 1
    But why tho
  • 1
    This is NOT a quirk of JS, it's you misunderstanding the map function.

    Here's your hit to why this happens:

    ['1','2','3'].map(function(){console.log(arguments)})
  • 3
    ['1', '7', '11'].map(e => parseInt(e));

    There, I fixed it.
  • 1
    Oh I know full well why it does what it does... but still it's not a trivial mechanic even to seasoned JS developers.

    You might call it quirky...
  • 1
    @metamourge
    See, if he was using webstorm, it would be screaming at him "USE THE RADIX, DERP"
  • 1
    @SortOfTested WebStorm is kind of too intrusive with things like this and IMO should relegate that to linters.
  • 1
    @kamen
    I don't disagree. Thankfully it's configurable.
  • 1
    @kamen Arrow functions. <3
    I will design an arrow function based lightsaber one day. You see, " =>{ " looks like the cut of a blade emitter.
    (______)=>{-pshiuummmm->
  • 1
    God fucking damn it
    This bullshit again
    This is the third time I see this here within ONE WEEK
    READ THE GOD DAMN DOCUMENTATION OF parseInt!!!
  • 1
    @PrivateGER, I did. You should read the comments before jumping to conclusions. I know what it does and why it does it but it's still a wierd line of code.
Add Comment