2

When try something like this in JavaScript ['10','10','10'].map(parseInt)

You will get this is as a result: [ 10, NaN, 2 ]

Why does this happen?

nodebase
  • 2,510
  • 6
  • 31
  • 46
  • 5
    http://stackoverflow.com/questions/262427/javascript-arraymap-and-parseint – Sougata Bose Nov 05 '14 at 03:35
  • Valid second arguments for `parseInt` is radix parameter from 2-36 (i.e., base 2 to base 36). `map` method of array passes the index as this second argument causing an error for index=1, but apparently not index=0 since that is interpreted as a null or undefined which 'mostly' defaults to base 10 depending on the string prefix ("0" or "0x"). – jongo45 Nov 05 '14 at 03:46

1 Answers1

3

parseInt is often used with one argument, but takes two.

The first is an expression and the second is the radix.

To the callback function, Array.prototype.map passes 3 arguments to parseInt: the element, the index, the array.

The third argument is ignored by parseInt, but not the second one, hence the possible confusion.

nodebase
  • 2,510
  • 6
  • 31
  • 46