Monday, May 20, 2024
43
rated 0 times [  46] [ 3]  / answers: 1 / hits: 15612  / 12 Years ago, fri, january 25, 2013, 12:00:00

Possible Duplicate:

javascript - Array.map and parseInt






I saw this example of strange JavaScript behavior on twitter



['10','10','10','10','10'].map(parseInt)


evaluates to



[10, NaN, 2, 3, 4]


could somebody explain this behavior? I verified it in chrome and firebug



['10','10','10','10','10'].map(function(x){return parseInt(x);})


correctly returns an array of 10s as integers. Is this an improper use of map(), a bug with parseInt, or something else?


More From » functional-programming

 Answers
5

parseInt receives two arguments: string and radix:




var intValue = parseInt(string[, radix]);




while map handler's second argument is index:




... callback is invoked with three arguments: the value of the element,
the index of the element, and the Array object being traversed.



[#80618] Thursday, January 24, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ravenl

Total Points: 338
Total Questions: 107
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
ravenl questions
Thu, Feb 18, 21, 00:00, 3 Years ago
Tue, Jan 12, 21, 00:00, 3 Years ago
Tue, Mar 17, 20, 00:00, 4 Years ago
;