5

I have read Armin Ronacher's Stop Being Cute and Clever article, where he gives this example of strange implementation of .map() in JavaScript, but he has no explanation for this and I am quite curious, because it does not make sense to me. So, why

console.log(["1", "2", "3"].map(parseInt))

outputs:

[ 1, NaN, NaN ]
exebook
  • 32,014
  • 33
  • 141
  • 226
  • Funny, but this is working: `console.log(["1", "2", "3"].map(function(el) { return parseInt(el); }))`. – Tony Dec 12 '13 at 08:53
  • 7
    Because `parseInt` takes a second parameter that `map` expects to be the index, but it's not, it's the "radix". On the other hand, `parseFloat` will work just fine. – elclanrs Dec 12 '13 at 08:54
  • 1
    The oldest dupe is actually http://stackoverflow.com/questions/262427/javascript-arraymap-and-parseint and it has an excellent accepted answer. – Álvaro González Dec 12 '13 at 08:55

0 Answers0