News for hackers. Stuff that matters.

hoblaypop

Re:FP techniques that will help you write better JavaScript on Friday February 11, 2022 @11:58AM (#30303735)
From https://stackoverflow.com/questions/22036300/what-is-1-2-3-m...

// Consider: ["1", "2", "3"].map(parseInt); // While one could expect [1, 2, 3] // The actual result is [1, NaN, NaN]

// parseInt is often used with one argument, but takes two. The second being the radix // To the callback function, Array.prototype.map passes 3 arguments: // the element, the index, the array // The third argument is ignored by parseInt, but not the second one, // hence the possible confusion. See the blog post for more details

function returnInt(element){ return parseInt(element,10); }

["1", "2", "3"].map(returnInt); // Actual result is an array of numbers (as expected)