Explain to me the result of this expression in Javascript:
["10", "10", "10", "10"].map(parseInt)
Explain to me the result of this expression in Javascript:
["10", "10", "10", "10"].map(parseInt)
For further actions, you may consider blocking this person and/or reporting abuse
Ben Halpern -
Code of Relevancy -
Medea -
Ben Halpern -
Once suspended, theodesp will not be able to comment or publish posts until their suspension is removed.
Once unsuspended, theodesp will be able to comment and publish posts again.
Once unpublished, all posts by theodesp will become hidden and only accessible to themselves.
If theodesp is not suspended, they can still re-publish their posts from their dashboard.
Once unpublished, this post will become invisible to the public and only accessible to Theofanis Despoudis.
They can still re-publish the post if they are not suspended.
Thanks for keeping DEV Community safe. Here is what you can do to flag theodesp:
Unflagging theodesp will restore default visibility to their posts.
Top comments (6)
parseInt()
takes to arguments, astring
and aradix
. I usually pass just the string I want converting as a number, you are passing both the string and the radix!map
gives us the value and the index (and the actual array)By just doing
.map(parseInt)
parseInt
is taking both the value and the index.Basically the second iteration is
parseInt('10',1)
and returnsNaN
.Finally, to test that that's what happens, the following would return
"0: 10" "1: 10" "2: 10" "3: 10"
This is because from Array.prototype.map
Also from parseInt()
So when this is executing it will return
take a list of strings that represent numbers and return a new list with all the strings converted to the numbers they represent, but they now have the data type of number
if you execute this statement, the result will be [10, NaN, 2, 3]. Why?
But if you use parseFloat instead of parseInt, you will get the list of numbers.
I can't really explain why this is happening. But you can use this one as a replacement:
whoa, you're right. Can someone explain what is happening?
I usually write map functions like this
and that does indeed return [10, 10, 10, 10].
passing just parseInt as a callback.. how does it know what arguments to take?
This answer on StackOverflow gives the correct reasoning: stackoverflow.com/a/262511/910328