DEV Community

Trudy
Trudy

Posted on

['10','10','10','10','10'].map(parseInt) what&why?

the answer is [10, NaN, 2, 3, 4]

['10','10','10','10','10'].map(parseInt) is equal

['10','10','10','10','10'].map((item, index) => parseInt(item, index))

here is detail about parseInt

Top comments (2)

Collapse
 
tracygjg profile image
Tracy Gilmore

Hi Trudy,

Your second example ['10','10','10','10','10'].map((item, index) => parseInt(item, index)) is not quite correct.

['10','10','10','10','10'].map(parseInt);
Enter fullscreen mode Exit fullscreen mode

is actually equivalent to

['10','10','10','10','10'].map((item, index, arr) => 
  parseInt(item, index, arr));
Enter fullscreen mode Exit fullscreen mode

but parseInt actually only expects two parameters so the third argument is ignored. This is because the map method expects a callback with three parameters. See

Regards, Tracy

Collapse
 
__e4b5e758a62e7c9e149c profile image
Trudy

You explained very well, more detailed and specific,thank you