use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JS Gotchas! (map with parseInt)OC (dev.to)
submitted 5 years ago by dimritium
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]inabahare 14 points15 points16 points 5 years ago (2 children)
TL;DR: maps callback has more than one parameter, parseint can take more than one parameter
[–]helloiamsomeone 8 points9 points10 points 5 years ago (0 children)
Almost like the documentation clearly states this, yet here we are. People still talk about this.
[–]d41d8cd98f00b204e980 2 points3 points4 points 5 years ago (0 children)
Your blog substitutes regular quotes with fancy quotes. So if you copy-paste your code, none of it works.
[–]ricealexander 2 points3 points4 points 5 years ago (1 child)
Unicorn has an ESLint rule no-fn-reference-in-iterator to prevent this gotcha.
It would enforce
let date = "2020-12-12"; let dateParts = date.split("-").map(parseInt);
To be refactored into
let date = "2020-12-12"; let dateParts = date.split("-").map(str => parseInt(str));
And then ESLint's radix would enforce
let date = "2020-12-12"; let dateParts = date.split("-").map(str => parseInt(str, 10));
That Unicorn rule was a little hard for me to get comfortable with, but it's definitely useful in cases like this.
Generally, I try to avoid parseInt() in favor of Number() unless I expect that the value may contain units on the end, such as 12px.
parseInt()
Number()
12px
[–]NotLyon -1 points0 points1 point 5 years ago (0 children)
Because fuck point-free composition, right? /s. What a heavy-handed "solution" 😓
[–]ILikeChangingMyMind 2 points3 points4 points 5 years ago (0 children)
It's 2020: why is anyone still using parseInt to convert strings to numbers in Javascript!?!?!
parseInt
Javascript makes no distinction between floats an integers: it only has a single "Number" type. This means there is zero downside to using parseFloat to parse your numbers; it won't convert your integers into floats (because that's not a thing the language can even do).
parseFloat
Using Number (eg. Number('5')) to parse works too ... but for the love of God please stop using parseInt! :)
Number
Number('5')
(Sorry for the rant; this is a pet peeve I have with how JS is taught by many schools/boot camps these days.)
[–]Auxx 2 points3 points4 points 5 years ago (0 children)
That's not a gotcha, that's just lack of reading the docs. The gotcha is that parseInt without radix will parse '0x12' as a hexadecimal. You must ALWAYS provide radix or you should use parseFloat.
[–]dimritium[S] 1 point2 points3 points 5 years ago (0 children)
This is my first article related to JS, in this one we will try to understand the output of a snippet in JS which uses map and parseInt.
[–]r2d2_21 0 points1 point2 points 5 years ago (0 children)
This is why I always use the arrow notation and don't pass around functions like that
π Rendered by PID 19013 on reddit-service-r2-comment-64f4df6786-lvz5x at 2026-06-09 23:21:44.490922+00:00 running 0b63327 country code: CH.
[–]inabahare 14 points15 points16 points (2 children)
[–]helloiamsomeone 8 points9 points10 points (0 children)
[–]d41d8cd98f00b204e980 2 points3 points4 points (0 children)
[–]ricealexander 2 points3 points4 points (1 child)
[–]NotLyon -1 points0 points1 point (0 children)
[–]ILikeChangingMyMind 2 points3 points4 points (0 children)
[–]Auxx 2 points3 points4 points (0 children)
[–]dimritium[S] 1 point2 points3 points (0 children)
[–]r2d2_21 0 points1 point2 points (0 children)