Skip to content

Instantly share code, notes, and snippets.

@luc4leone
Last active September 30, 2019 10:40
Show Gist options
  • Save luc4leone/1bb18ce811245f1011d6dd734d022b18 to your computer and use it in GitHub Desktop.
Save luc4leone/1bb18ce811245f1011d6dd734d022b18 to your computer and use it in GitHub Desktop.
['1', '2', '3'].map(parseInt)
// ['1', '2', '3'].map(parseInt) returns [1, NaN, NaN]; why?
// simplified `map`, to be able to step into the code through the debugger
function map(array, callback) {
var mappedArray = [];
for (var i = 0; i < array.length; i++) {
mappedArray[i] = callback(array[i], i, array);
}
return mappedArray;
}
debugger;
var result = map(["1", "2", "3"], parseInt);
console.log(result); // [1, NaN, NaN]
// compare with
var result2 = map(["1", "2", "3"], function(n) {return parseInt(n)});
console.log(result2); // [1, 2, 3]
// side experiment: what happens when we pass an extra argument to a function
function logName(first, second, third) {console.log(first, second, third)}
logName("first", "second", "third", "fourth"); // first second third
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment