uechi.io/source/_posts/2021/parseint-magic.md
2021-02-14 15:47:14 +09:00

623 B

title date
[].map(parseInt) 2021-02-14T11:30:00

Fun fact: [0xa, 0xa, 0xa].map(parseInt) yields [10, NaN, 2].

Why

parseInt(0xa, 0, [0xa, 0xa, 0xa]);

The second argument is 0 so the first argument going to be treated as decimal number becoming 10.

parseInt(0xa, 1, [0xa, 0xa, 0xa]);

The second argument is 1 which is invalid as a radix, so the result ends up with NaN.

parseInt(0xa, 2, [0xa, 0xa, 0xa]);

The second argument is 2 meaning the first argument going to be handled as a binary number. 0xa is 10 in binary, which results in 2 in decimal form.