Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Save time
parseInt is often used with one argument, but takes two.
The first is an expression and the second is 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.
here is concise example of the iteration steps:
parseInt(string, radix) -> map(parseInt(value, index))
- First iteration (index is 0): parseInt('1', 0)
results in parseInt('1', 0) -> 1
- Second iteration (index is 1): parseInt('2', 1)
results in parseInt('2', 1) -> NaN
- Third iteration (index is 2): parseInt('3', 2)
results in parseInt('3', 2) -> NaN
So just use a proper callback if you are not sure
["1", "2", "3"].map(n => parseInt(n)); -
-
@Techno-Wizard That's one. The other is not throwing an error when trying to hand over a string as radix.
-
blem1410636y@milkyway ok, it has some sense, but at the same time it does not...
I mean, comming from any other, sane language, you expect the map to call given function with element as argument, but then map calls it with at most 3 args if function will accept it... I see that it may be useful in some cases, but.... most of the times it won't, I guess.
The more I hear about JS the more I see its developers are like "Oh, no, it's not wrong at all. It allows you to do more than other languages. It has no sense? Oh cmon, just learn it by heart, you'll be all right, mate!". -
@blem14 like any sane developer would read the doc before whining.
parseInt and parseFloat exist since the dawn of time unlike .map which is fairly recent. Their behaviour cannot be changed without breaking countless websites and therefore stay as is.
As for map prototype, I've never used the reference to the original array but the index is however extremely useful.
@Fast-Nop sure but how is that related to op's snippet? The radix is always a number here. Or am I missing something? -
@Commodore "cannot be changed without breaking behavior"
Nope. Do what every other language has done: update the standard and have an HTML header attribute for version. -
@Techno-Wizard Terrible idea. If anything, the script itself would have to include its version.
-
Boogie3336y@Techno-Wizard How about adding function "parseInteger", depreacte "parseInt" and recommend using the former? It would be less confusing. ;)
-
Technically 1 is the only real number.
0 is the absence of number and 2 is just 1 added together.
Actually javacript knows a lot more than you do... Because you know nothing... -
Trithon10295y.map( c => parseInt( c ) )
Works.
ParseInt takes two parameters (string and base)
Map gives three parameters: current value, index and the array.
ParseInt (1,0) = 1 //0-base == auto
ParseInt (2,1) = NaN //base 1 doesnt work
ParseInt (3,2) = NaN //3 is not binary -
josap2115yHow the fuck has this rant over 147 ++ ???
This is something that's probably answered 100s of times on SO (=> elementary)
Related Rants
That's why I never believe common sense when I write Javascript.
rant
map
int
float
js
javascript
integer