denysdovhan / wtfjs

🤪 A list of funny and tricky JavaScript examples
http://bit.ly/wtfjavascript
Do What The F*ck You Want To Public License
34.75k stars 2.55k forks source link

Mapping with parseInt over arrays #211

Open adrfantini opened 3 years ago

adrfantini commented 3 years ago

I feel like this classic gem should also be included (if it is not there already and I missed it):

[1,2,3].map(parseInt)
// [ 1, NaN, NaN ]

Explanation: map passes three arguments to parseInt for each iteration, and parseInt accepts the second argument (undefined) as radix, and fails silently. [1,2,3].map((x) => parseInt(x)) works.

Links:

Doc:

denysdovhan commented 3 years ago

Could you send a PR?