Earlier quoted context omitted.
A typed language would absolutely fail this at compile time.
Only because map's third provided argument exists. If map only supplied element and index to the callback, this would pass most type checkers I'm familiar with, and would remain just as confusing.
Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
81–90 of 163 posts
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#82 [5 * 5] * 2 // 50
[5 + 5] * 2 // NaN
1 + [5 * 5] * 3 // 76
1 + [5 * 5] - 1 // 124
Interestingly, even Typescript will not ( by default ) catch this class of bugs.Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#83This 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.
[...].map(Number)
?
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#84You can crank up the settings in TsLint and never worry about things like this again
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#85Earlier quoted context omitted.
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…
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.
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 JavaScript
#86Earlier quoted context omitted.
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 su…
> 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, whe…
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#87This 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.
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 su…
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 JavaScript
#88Earlier quoted context omitted.
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 su…
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 JavaScript
#89Earlier quoted context omitted.
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 su…
> 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, whe…
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#90Not too weird IMO.