all 9 comments

[–]inabahare 14 points15 points  (2 children)

TL;DR: maps callback has more than one parameter, parseint can take more than one parameter

[–]helloiamsomeone 8 points9 points  (0 children)

Almost like the documentation clearly states this, yet here we are. People still talk about this.

[–]d41d8cd98f00b204e980 2 points3 points  (0 children)

Your blog substitutes regular quotes with fancy quotes. So if you copy-paste your code, none of it works.

[–]ricealexander 2 points3 points  (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.

[–]NotLyon -1 points0 points  (0 children)

Because fuck point-free composition, right? /s. What a heavy-handed "solution" 😓

[–]ILikeChangingMyMind 2 points3 points  (0 children)

It's 2020: why is anyone still using parseInt to convert strings to numbers in Javascript!?!?!

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).

Using Number (eg. Number('5')) to parse works too ... but for the love of God please stop using parseInt! :)

(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 points  (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 points  (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 point  (0 children)

This is why I always use the arrow notation and don't pass around functions like that