News for hackers. Stuff that matters.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by roryrjb on Friday June 21, 2019 @10:58AM (#20243040)
This isn't strange or surprising. parseInt takes two arguments, the second one is the radix and map will call with three arguments, the value, the index and the whole array. You just have to know this and it might be different in other languages. [ ... ].map(x => ...) is the right way to do this.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sametmax on Friday June 21, 2019 @12:07PM (#20243765)
It may be logical but:

- I know all that, have been programming for 15 years, yet I still would have done the mistake.

- Simple unit tests may very well not catch this bug the first time.

- The language design allows your brain to ignore the index parameter because JS accepts superfluous parameters, which is a terrible decision.

- map() is a mapping primitive. It's supposed to adapt a type to another type so that you can pass it to a monoid. JS breaks this convention.

- Even traditionally not functional languages know better than that and separate iteration from indexing. E.G: python map() just maps, and if you want numbering, you use explicitly enumerate(). Ruby has each() and each_with_index(), etc.

Just another of the numerous sucky things in JS. They are not big, but they accumulate very quickly and make it one of the worse language existing. Certainly the worst of all modern stack languages. It's a shame it has a monopoly on the most awesome platform in the world: the web.

In fact, it's such a big problem the most popular JS projects are all things to avoid coding in JS or workaround JS deficiencies: typescript, coffeescript, jsx, babel, webpack, lowdash ...

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by jacobolus on Friday June 21, 2019 @12:26PM (#20243958)
> I know all that, have been programming for 15 years, yet I still would have done the mistake.

I have not done it with parseInt per se, but I have made this precise mistake at least twice with passing a function `f` with an optional second argument in `some_array.map(f)`, and it took a while to figure out what was happening each time.

Now that Javascript has proper iterables and iterators and generators, I have been enjoying using versions of `map` and `spreadmap` which take in a callback function and iterables and produce an iterator. Then I can explicitly use `enumerate` if I want it. https://observablehq.com/@jrus/itertools#map

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by Oreb on Friday June 21, 2019 @12:38PM (#20244084)
As a (mostly) outsider, I’ve never understood why JavaScript is so popular in web dev circles. There are so many awesome compile-to-JS languages these days (ClojureScript, PureScript, Elm, ReasonML, Scala.js, etc). What makes people want to use JavaScript instead?
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by RussianCow on Friday June 21, 2019 @12:46PM (#20244161)
Any time you use anything non-native, you add layers of indirection and potentially lots of impedance mismatch between languages/runtimes. For instance, I know all of the ones you mentioned would be non-starters at my current job because it means we would have to wrap all of our JS libraries/APIs in the language of choice, which is a non-trivial amount of work. Plus, debugging becomes more challenging as you're basically having to do it at two levels simultaneously.

Even with all of JavaScript's deficiencies, it's still far easier to just deal with them than to switch languages entirely.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by wutbrodo on Friday June 21, 2019 @12:46PM (#20244163)
It's permissiveness means you can hit the ground running more easily writing shitty but functional code, and web dev has long been the gateway for new entrants to engineering. People start programming, learn shitty habits from a shitty language ,and then the religious affinity for programming languages that many have starts to kick in.

This obviously doesn't explain every Javascript fan, but the tendency is strong enough to shift the dynamics of the community.

I can't say I relate to the religious fervor with which people defend programming languages. C++ is probably my favorite language to work in, but I absolutely understand where the hatred for it comes from. I readily admit that it's grotesque in many ways and my preference is likely for lack of sustained exposure to certain other modern languages.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by Splognosticus on Saturday June 22, 2019 @04:14AM (#20248569)
Probably just because it's there--you can start writing JavaScript without installing anything, same as you used to be able to do with BASIC on 8-bit micros. Then when you consider how much Visual Basic or VBA code there must be in the world and why that is you can start to see how the JavaScript ecosystem is how it is.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by Shish2k on Friday June 21, 2019 @05:10PM (#20246325)
> Certainly the worst of all modern stack languages

So have you not used PHP, or are you counting it as not-modern? :P

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sametmax on Saturday June 22, 2019 @01:17AM (#20248150)
I've started my career as a PHP dev. Even if you consider them equally bad, at least PHP has a stdlib and no prototypal inheritance.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by megous on Friday June 21, 2019 @12:18PM (#20243866)
> - The language design allows your brain to ignore the index parameter because JS accepts superfluous parameters, which is a terrible decision.

Without that we would not be able to have variable length argument lists in the past.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sametmax on Friday June 21, 2019 @12:24PM (#20243925)
Yes, but again that's the consequence of another bad design: not including spread or another metaprogramming system for this. And not having default values either.

Spread has existed in Python forever under the name of "splat operator", default values as well. Same with ruby.

That what I meant when I said "they accumulate". A bad design decision not only affect the user cognitive load and productivity, but it also cascades to the rest of the language and shapes it.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by o_nate on Friday June 21, 2019 @12:56PM (#20244260)
Whenever I learn about another of these Javascript foot-guns, I come away with a greater sense of awe at the relentless good sense that went into the design of Python. Not sure if it's a kind of super-human Dutch ability to foresee ramifications of design decisions, or if it's just that Python developed organically over a period of time with healthy feedback from smart and invested users.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sametmax on Friday June 21, 2019 @01:06PM (#20244363)
It's also about the ability to say "fuck off".

People came at Guido relentlessly for adding new features, debating his decisions, trying to change the philosophy and aesthetic of the language.

The guy stood up to them for 20 years, staying polite but resolute at doing things his, at the time controversial, way.

To me, that's even more impressive than being a good language designer.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by jacobolus on Friday June 21, 2019 @01:00PM (#20244298)
> in Python forever under the name of "splat operator"

The name “splat” is not commonly used in Python. That name comes from Ruby (or Perl?). In Python it is usually called “star” or similar.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sametmax on Friday June 21, 2019 @01:16PM (#20244481)
Both are used.

Here you can see Steven D'Aprano, one of the dev of the Python stdlib, naming it "splat": https://mail.python.org/archives/list/[email protected]...

I use "splat" all the time myself.

On the other hand, itertools.starmap() is named this way because it does:

    def starmap(func, iterable):
        for e in iterable:
            yield func(*iterable)
Things rarely have one name in computing. Hell I call curly brackets "mustaches" all the time. It's as hard as cache invalidation apparently.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by jacobolus on Friday June 21, 2019 @02:27PM (#20245116)
That’s a mailing list email from 2016, and is a joke about bunch of silly non-standard names for punctuation (e.g. “bang”, “wack”, and “twiddle”).

Obviously occasional people are going to pick up terminology from other communities, and the name “splat” has been gaining popularity recently (I had literally never heard that term before a few years ago, and have been writing Python code since 2002). I occasionally hear British expats in the USA calling elevators “lifts” or baby carriages “prams” or lines of people “queues”. Doesn’t mean those have been common American terms.

This was certainly not called the “splat operator” “forever” as claimed in the previous comment.

> I call curly brackets "mustaches" all the time

I have never heard anyone call these “mustaches”. The common terms are “curly brackets” and “braces”. Nobody is going to have any idea what you are talking about.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sametmax on Friday June 21, 2019 @03:41PM (#20245709)
The most famous JS template engine of the early 2000 used to be literally called "mustache" because of it:

https://mustache.github.io/

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by jacobolus on Friday June 21, 2019 @08:59PM (#20247381)
Your reply continues the trend of factual sloppiness. Mustache is not the “most famous”, not “JS”, and not “of the early 2000[s]”.

Mustache is a Ruby library from 2009. Here’s the first commit https://github.com/mustache/mustache/commit/6ee6bcf21d381554...

But more to the point, someone calling their template library “mustache” because a curly brace has a vaguely mustache-like shape doesn’t remotely imply that people regularly call curly braces “mustaches” or would have any idea what “mustache” meant in the context of someone pronouncing their computer code.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by matharmin on Friday June 21, 2019 @11:12AM (#20243203)
The surprising bit comes in with the fact that: 1. parseInt is used with only a single argument in most cases. 2. Only the first argument of map is used in most cases.

Using multiple parameters for those are so uncommon that it's easy not to realise that it's supported.

_If you know all of that_ it may not be surprising to you, but in most cases there is no need for the average developer to know it, making it very surprising when it crops up like this.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by lucideer on Friday June 21, 2019 @11:37AM (#20243431)
> in most cases there is no need for the average developer to know it

This may come off as arrogant but.. I'll proceed nonetheless. There is a need for the average developer to know the interfaces of the functions they're choosing to use. They're well documented, and the documentation is neither hard to find, nor difficult to read.

There may be an argument here for strongly typed languages -vs- weakly typed ones, where often some of the ambiguity around optional function parameters is eased, but most languages have optional function parameters, this is not in any way unique to JS. And parseInt and [].map are very common JS methods: this is not some obscure API not all devs would be unfamiliar with, these are built-ins.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by wutbrodo on Friday June 21, 2019 @12:53PM (#20244224)
Instead of thinking about code writing, think about code reading. Every organization has people who wrote buggy code, for a million reasons: they were distracted, they were hurried, they simply aren't that good (at either engineering or the language). Sometimes this stuff gets through review too, particularly in situations with deadlines.

But one of the safeguards here is that clean, intuitive code makes it possible for any engineer who's a little more thoughtful to catch little bugs like this. God knows I've done it dozens of times over the course of my career: while reading code for some other purpose, a block catches my eye as having something off about it, and I dig in and find a bug.

The problem with unintuitiveness,especially when it's baked into the language, is that you don't scrutinize every line of code you encounter with your full brain and attention. Without going on the hunt for "map and Javascript in general are horribly designed, parseInt has a rarely-used second param, root out likely bad uses", my brain on another task would skim right over a map-parseInt call like the above. It reads as if it's correct, and in any sane language it would be.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by kbp on Friday June 21, 2019 @12:05PM (#20243733)
The part that is relatively unique to JS is where extra arguments passed to a function are silently ignored. In most every other language, the function passed to map takes 1 argument, which is also the common case in JS. But JS always passes 3 arguments to that function. I don't think it's optional parameters that people find confusing, it's the interplay between optional parameters and functions whose interface depends on functions they accept as arguments ignoring extra arguments.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by egeozcan on Friday June 21, 2019 @12:00PM (#20243684)
I don't understand why this is downvoted to death without any replies. Sounds like a reasonable opinion but I'd also love to hear why people think it's downvote-worthy.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by guitarbill on Friday June 21, 2019 @12:50PM (#20244200)
Not a downvoter, but I'll have a go:

You might be a dev who isn't very familiar with Javascript, but you still need to fix it. Or you might be having an off day. Or you're writing a critical fix under a lot of pressure and simply forget about the issue. And I'll bet if this code was being reviewed, most devs would still overlook the issue.

There are many real world scenarios where it isn't so easy or clear cut. We're all human, and we make mistakes. In other fields, we try and reduce how easy it is to make these mistakes. Some things will always be dangerous like table saws. But a programming language?

Anyway, the "deal with it"/"get good" mentality just seems like a lack of empathy. Not to mention the arrogant in-group thing of "oh, you don't know <wart x> of my favourite but flawed language? you must not be a good dev". And the fact that fixing something like this could cumulatively save a huge amount of brainpower for current and future devs.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by wutbrodo on Friday June 21, 2019 @12:58PM (#20244273)
The hilarious thing about the get-good arguments is that it's proponents don't realize that they're talking about expending effort _for nothing in return_, especially in cases like the server-side where nobody is forced to use Javascript.

You can say the same thing to Pythonistas who are fussed by memory management, but the tradeoff of that intuitiveness is performance (which is being somewhat obviated by newer languages like Rust, which have their own challenges to learn). The actual task you're trying to do is more complex, so the knowledge required is too.

In Javascript's case, the thing you're becoming an expert in is the path-dependent history of dumb language decisions by people who never should have been let anywhere near a language. It's beyond me that people don't understand why this would bug some, at the very least because it was so easily avoidable.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by acdha on Friday June 21, 2019 @11:29AM (#20243364)
There's some interesting history for awareness of the first issue: parseInt used to be a notorious source of bugs and so it used to be more common to see it with a second argument on any project which used a linter in that era.

ES5 deprecated that behaviour and it's been widespread enough that code which started in the IE9+ era now looks like the very old buggy code omitting the radix, so anyone getting started relatively recently quite reasonably may never have needed to learn about it:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... https://eslint.org/docs/rules/radix

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sdegutis on Friday June 21, 2019 @12:10PM (#20243798)
I didn't know about the second parameter until I saw it in a TypeScript signature a few months ago, which acted as a sort of warning to me to avoid using it incorrectly.

But TypeScript doesn't catch this specific bug because it's technically correct[1] from an API stand-point, as the second parameter to parseInt is a number and the second argument to map's callback is a number.

For as many situations as TypeScript genuinely helps you, there are just as many situations where it gives you false security, like in this case, and I'm starting to consider not using it anymore. [2]

[1] not actually "the best kind of correct" despite popular television quotes

[2] https://sdegutis.com/2019-06-20-considering-removing-typescr...

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by carlmr on Friday June 21, 2019 @12:26PM (#20243950)
Yeah, the problem this one time isn't typing but the JS developers implementing map wrong. Map should not take two arguments. There should be a different function like enumerate in in python for this.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sdegutis on Friday June 21, 2019 @01:34PM (#20244648)
There's nothing wrong with Array#map passing callback(val, i, array), it's a legitimate feature, and the two other arguments can be omitted in actual callbacks. It just means we need to be aware of how it works, which is basically true about any programming language, any standard library, and basically any kind of tool that any kind of professional uses, whether abstract or literal: know how to use your own tools, and avoid blaming the wrench because it's a poor excuse for a hammer.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by acdha on Friday June 21, 2019 @02:15PM (#20245005)
> know how to use your own tools, and avoid blaming the wrench because it's a poor excuse for a hammer

This is, of course, why famously machine shops simply put up signs saying “don't touch the whirring metal” rather than using safety guards and similar measures.

I don't disagree that it's useful to have the extra arguments for map but the part which is really deadly is ignoring extra arguments, even if it's obvious why backwards-compatibility made changing it unlikely. I really wish that JavaScript had implemented keyword-arguments when the benefits had become obvious because when features like map were added a decade or two later they could have been defined as passing arguments by keyword and causing an error when given a function which didn't accept those names.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by plausibilities on Friday June 21, 2019 @12:01PM (#20243691)
Why would you use parseInt over Math.round if you only expect a single arg?

Seems like you'd only want to use parseInt if you expect to need radix changes at some point, e.g. converting between hex strings, decimal values, and binary strings

    ['1', '7', '11'].map(Math.round)
    // => [1, 7, 11]

    [["00000001", 2], ["00000111", 2], ["0x0B", 16]].map(x => parseInt(...x))
    // => [1, 7, 11]
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by paulgb on Friday June 21, 2019 @12:03PM (#20243713)
To someone reading the code, parseInt actually describes what your intent is while Math.round just happens to work because it coerces the type.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by bemmu on Friday June 21, 2019 @12:06PM (#20243746)
If you want to get terse

    ['1', '7', '11'].map(x => +x)
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by plausibilities on Friday June 21, 2019 @02:46PM (#20245271)
If I didn't care about rounding I'd just use a primitive constructor

    ['1', '7', '11'].map(Number)
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by jacobolus on Friday June 21, 2019 @12:21PM (#20243891)
This coerces to a number, but not to an integer.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by reaktivo on Friday June 21, 2019 @01:30PM (#20244614)
I would hate to see it in code that is being shared in a team, but the following works:

['1', '7', '11'].map(x => ~~x)

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by jmull on Friday June 21, 2019 @11:36AM (#20243416)
Well, the average developer should use a lint tool with Javascript.

A bunch of confusion points in Javascript (including this one) have been well understood for a long time, best practices have been developed to deal with them, and mature tooling exists to easily enforce them.

Put another way: If you are unsure, go download eslint along with the plugins for your environment, and find/create a ruleset that turns on almost everything.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by wutbrodo on Friday June 21, 2019 @12:42PM (#20244119)
> This isn't strange or surprising.

> You just have to know this

Hidden, silent, unintuitive behavior that you "just have to know" (and remember each time you might read or write the code) is absolutely strange and surprising in an engineering context. Javascript's deadly combination of unnecessary unintuitiveness (map's ludicrous API) and permissibility (ignoring extra arguments) strikes again.

The permissibility of guessing what malformed code means is actually somewhat defensible in Javascript's case, since the incentive ecosystem of the Web is complex and frontend parsing has long had a tradition of best-effort parsing instead of throwing an exception. But the absurd signature of map here is half the problem, and there's really no good explanation for that than yet another instance of "Javascript is a dumpster fire that should only be used when forced to". Similarly "easy-to-use" languages like Python can afford strong typing and rejecting extra args when not specified because they're not bound by the Web's permissibility. But Python also, for all its flaws, has adults making language decisions and does the sane thing with APIs like map without loss of expressiveness. Adding loop metadata like indices requires an explicit call to enumerate(), trading off an iota of verbosity for intuitiveness, which is one of the most important things in writing code that can remain productive and bug-free.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by mpolichette on Friday June 21, 2019 @11:01AM (#20243082)
Yeah, I’m not surprised at all by this. I get that there are a lot of weird things in JS but I don’t think this is a JS oddity.

Surprise! You need to know the basics of how your language works.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by arcticbull on Friday June 21, 2019 @11:21AM (#20243283)
That's apologist talk pure and simple. The language, silently, does something that's almost certainly wrong. The language has enough information to provide you with a helpful warning or error message that you probably don't want to do this, but instead, it violates the principle of least surprise by just doing the wrong thing instead.

The correct error is something along the lines of:

  let numbers = input.iter().map(parseInt).collect::<Vec<_>>();

    --> src/main.rs:10:32
     |
  4  | fn parseInt<T>(input: T, radix: u32) -> Option<i32> where T: AsRef<str> {
     | ----------------------------------------------------------------------- takes 2 arguments
  ...
  10 |     let numbers = input.iter().map(parseInt).collect::<Vec<_>>();
     |                                ^^^ expected function that takes 1 argument
This isn't rocket science, it's basic language design. From time immemorial engineers have been making mistakes as they write software, so compilers evolved not to pretend otherwise and hope for the best, but to help engineers catch them. Except for one.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by pennaMan on Friday June 21, 2019 @11:24AM (#20243316)
The map function fully expects a function with two arguments, as per documentation. Why would passing a function that takes two arguments to map be an error?
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by estebank on Friday June 21, 2019 @02:22PM (#20245073)
The map function has a bad API surface that interacts poorly with the way people expect it to work. In a better designed API, if you needed the index of each yielded value other languages have some kind of enumerate method that will allow people opt-in to getting the index. Then if you needed the replicate the presented situation you would have something like

    ['1', '7', '11'].enumerate().map(([val, idx]) => parseInt(val, idx))
I understand that this pattern of having extra "helper" arguments is pervasive in JS and might seem less exotic to the community, but I feel it is a problematic approach, as it is foot-gun prone.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by benschulz on Friday June 21, 2019 @11:36AM (#20243418)
The example given is taken from Rust. Rust does not allow overloading (even in terms of optional parameters, I believe). Well, Rust allows overloading by implementing different traits but then forces you to disambiguate where necessary.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by nwellnhof on Friday June 21, 2019 @11:26AM (#20243331)
> The language has enough information

No.

> expected function that takes 1 argument

`map` expects a function that takes one, two, or three arguments.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by arcticbull on Friday June 21, 2019 @11:28AM (#20243348)
Well there's your first problem haha.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by fortran77 on Friday June 21, 2019 @11:54AM (#20243617)
Right! It should have a map, map2, map3 function
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by a1369209993 on Friday June 21, 2019 @01:41PM (#20244697)
Well, more like map, mapIndex, mapIndexSrc[0], but yes.

0: Going by JS naming conventions anyway; I'd use somthing like map, mapi, no-that's-useless personally.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by arcticbull on Friday June 21, 2019 @12:55PM (#20244246)
Nah, there's better solutions; you can create a window/stride method that converts a sequence into a sequence of sequences and then passes that into a map function that takes a single argument.

  array.stride(2).map(|(x,y) ...|)
Both error checking and ergonomics are preserved.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by estebank on Friday June 21, 2019 @12:41PM (#20244117)
If Rust had ergonomic support to pass multiple arguments to a functions you would still not be protected from this bug. Even the type system wouldn't help you there because the index would be of type usize and so would be the radix argument in parseInt (although in reality this would probably be an enum or u8). But this is caused by the interaction of two bad APIs. Even if we have the restriction that it confirms to the over two decades old signature, map should only yield each element of the input and yield back the result of the closure. If you need the index you could chain a call to enumerate before the map and get tuples of value and index. This is what you'd have to do in Rust and Python, for example.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by arcticbull on Friday June 21, 2019 @09:54PM (#20247554)
This isn't meant to be some zen gardening, however... sometimes not having something is itself a feature.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by nerdponx on Friday June 21, 2019 @11:21AM (#20243285)
IMO the real oddity here is that Javascript works like this.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by happytoexplain on Friday June 21, 2019 @11:35AM (#20243409)
I've developed applications in JS, but don't have extensive experience with it, and I couldn't spot the problem. Frankly, I think if `['1', '2'].map(parseInt)` is both valid and does not result in a collection containing the integers 1 and 2 in that order, that indicates a catastrophic failure of design at some level. These things happen, but usually you have to dig down to a less bog standard example to find it. The "you just have to know" argument is often unavoidably valid, but in this case I think it's way off the mark.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by lucideer on Friday June 21, 2019 @11:40AM (#20243463)
The problem is that this article is attributing this problem to JS, whereas the problem is with either computer science or human decimal bias: i.e. the radix is not a concept invented by JS.

If you think it should be ok for all devs to believe that parseInt === parseDecimalInt then maybe all languages should be decimal-only. That isn't the case though.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sixstringtheory on Friday June 21, 2019 @11:59AM (#20243675)
All languages I know of _default_ to a radix of 10 though, allowing other radixes where a prefix is added like 0b111010, 0xa7b45fd9, etc.

Seems intuitive that if parseInt allows omitting the radix parameter, that it should default to whatever radix a standard integer primitive would default to.

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by ianjsikes on Friday June 21, 2019 @12:18PM (#20243867)
> Seems intuitive that if parseInt allows omitting the radix parameter, that it should default to whatever radix a standard integer primitive would default to.

But in this case, the radix is _not_ omitted. The map function passes the index of the current iteration as the second parameter to the function it is passed. It is kinda like this: ``` ['1','7','11'].map((item, index) => parseInt(item, index))

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sixstringtheory on Friday June 21, 2019 @01:21PM (#20244536)
Thanks for connecting the dots for me, I see it now!
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by markdoubleyou on Friday June 21, 2019 @12:06PM (#20243754)
Yeah, this gotcha doesn't bother me nearly as much as the classic:

  [9, 10, 11].sort()

  [ 10, 11, 9 ]
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by otakucode on Friday June 21, 2019 @12:35PM (#20244055)
Map ever calling anything with 3 arguments is absolutely strange and surprising and should be borderline offensive to most anyone familiar with the operation from ANY other language.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by nkrisc on Friday June 21, 2019 @11:26AM (#20243340)
I'd certainly call it unintuitive if you run into it yourself and aren't aware of this, but once you unpack it we've seen it's expected behavior.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by sorokod on Friday June 21, 2019 @11:36AM (#20243421)
This can be said about pretty much any bug.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by crossman on Friday June 21, 2019 @11:00AM (#20243059)
Yeah.. Didn't even need to load the article. You're passing the index as the radix. Makes sense
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by ballenf on Friday June 21, 2019 @11:56AM (#20243640)
What's wrong with just:

[...].map(Number)

?

Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by jlarocco on Friday June 21, 2019 @11:06AM (#20243131)
So it's clickbait?
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by syntheticcdo on Friday June 21, 2019 @11:23AM (#20243309)
The article is not clickbait but the takeaway is easily misinterpreted. Instead of "JavaScript is such a weird and confusing language" it should be "a lot of people are using JavaScript without understanding the method signatures of commonly using methods".
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by tntn on Friday June 21, 2019 @11:46AM (#20243521)
a lot of people are using JavaScript without understanding the method signatures of commonly using methods because JavaScript is such a weird and confusing language that has nonstandard method signatures for standard functions.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by happytoexplain on Friday June 21, 2019 @11:41AM (#20243477)
Those sound like two sides of the same coin.
Re:Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in... by fgonzag on Monday June 24, 2019 @06:33PM (#20269206)
A lot of people are using JavaScript expecting it to conform to the principle of least surprise. Map applies a function f to each element and returns the result. If you want to supply a second argument, you either have to partially apply the function or manually supply the arguments. JavaScript's map is different from what the rest of the world calls map, and that's the problem.

I would never have guessed that it was passing the index as a second parameter, I would have expected a compiler error.