DEV Community

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

Collapse
 
nektro profile image
Meghan (she/her)
["10", "10", "10", "10"].map(parseInt)  ==>  [10, NaN, 2, 3]
Enter fullscreen mode Exit fullscreen mode

This is because from Array.prototype.map

map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results.
callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.

Also from parseInt()

Syntax: parseInt(string, radix);

So when this is executing it will return

[
    parseInt("10", 0),
    parseInt("10", 1),
    parseInt("10", 2),
    parseInt("10", 3)
]
Enter fullscreen mode Exit fullscreen mode