137
Boogie
5y

That's why I never believe common sense when I write Javascript.

Comments
  • 8
    You fucking what, mate?!
  • 18
    Oh this again
  • 65
    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));
  • 13
    @milkyway "the third arg is ignored"

    Ladies, gentlemen, and everyone else: there's your problem.
  • 7
    @Techno-Wizard That's one. The other is not throwing an error when trying to hand over a string as radix.
  • 3
    @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!".
  • 9
    @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?
  • 2
    @Commodore ah yeah sure that's the index, my fault.
  • 1
    @Commodore Wait til I get to php!
  • 0
    @Boogie by all means 😅
  • 2
    @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.
  • 2
    @Techno-Wizard which would be even more confusing to use. So no.
  • 1
    @Techno-Wizard Terrible idea. If anything, the script itself would have to include its version.
  • 0
    @Techno-Wizard How about adding function "parseInteger", depreacte "parseInt" and recommend using the former? It would be less confusing. ;)
  • 2
    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...
  • 0
    .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
  • 1
    You dense mofo, stop judging my js
  • 2
    @Fast-Nop The way python does it yeah...
  • 1
    Oh wow another idiot who doesn't know how to read the docs!
  • 2
    @Commodore ikr, it's the third one this week ALONE
  • 2
    How the fuck has this rant over 147 ++ ???
    This is something that's probably answered 100s of times on SO (=> elementary)
Add Comment