Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in Javascript

Eric Tong
Eric Tong
Jun 12, 2019 · 5 min read
Image for post
Image for post
???
['1', '7', '11'].map(parseInt);

Truthiness & Falsiness

if (true) {
// this always runs
} else {
// this never runs
}
if ("hello world") {
// will this run?
console.log("Condition is truthy");
} else {
// or this?
console.log("Condition is falsy");
}

Radix

0 1 2 3 4 5 6 7 8 9 10
DECIMAL   BINARY    HEXADECIMAL
RADIX=10 RADIX=2 RADIX=16
0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
16 10000 10
17 10001 11

Function arguments

function foo(x, y) {
console.log(x);
console.log(y);
}
foo(1, 2); // logs 1, 2
foo(1); // logs 1, undefined
foo(1, 2, 3); // logs 1, 2

map()

function multiplyBy3(x) {
return x * 3;
}
const result = [1, 2, 3, 4, 5].map(multiplyBy3);console.log(result); // logs [3, 6, 9, 12, 15];
[1, 2, 3, 4, 5].map(console.log);
Image for post
Image for post
[1, 2, 3, 4, 5].map(console.log);// The above is equivalent to[1, 2, 3, 4, 5].map(
(val, index, array) => console.log(val, index, array)
);
// and not equivalent to[1, 2, 3, 4, 5].map(
val => console.log(val)
);

Bringing it together

parseInt('11');                => 11
parseInt('11', 2); => 3
parseInt('11', 16); => 17
parseInt('11', undefined); => 11 (radix is falsy)
parseInt('11', 0); => 11 (radix is falsy)
['1', '7', '11'].map(parseInt);       => [1, NaN, 3]// First iteration: val = '1', index = 0, array = ['1', '7', '11']parseInt('1', 0, ['1', '7', '11']);   => 1
// Second iteration: val = '7', index = 1, array = ['1', '7', '11']parseInt('7', 1, ['1', '7', '11']);   => NaN
// Third iteration: val = '11', index = 2, array = ['1', '7', '11']parseInt('11', 2, ['1', '7', '11']);   => 3

Summary (TLDR)

['1', '7', '11'].map(numStr => parseInt(numStr));

Image for post
Image for post

DailyJS

JavaScript news and opinion.

Eric Tong

Written by

Eric Tong

I write about the intersection of design and development. Front-end developer, founder of TwentyFourLabs, student at Oxford University. eric-tong.com

DailyJS

DailyJS

JavaScript news and opinion.

Eric Tong

Written by

Eric Tong

I write about the intersection of design and development. Front-end developer, founder of TwentyFourLabs, student at Oxford University. eric-tong.com

DailyJS

DailyJS

JavaScript news and opinion.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store