Earlier quoted context omitted.
This has nothing to do with casts or defaults. Map passes multiple parameters and parseInt accepts multiple parameters. Being unaware of the functions you're using is the problem, not some sort of weird gotchas of the language. The code in the title makes multiple assumptions, and those assumptions all proved wrong.
I think the parent is talking about how C for instance does not have optional parameters (for better or worse), if this was the case for JS everyone who has ever used parseInt would be aware it takes a radix, but then again you also wouldn't be able to just plug it into map arbitrarily. Not saying one is better than the other, optional parameters can make for much less verbose code, I especially like parameter defaul…
Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
131–140 of 163 posts
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#132Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#133Earlier quoted context omitted.
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 g…
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 JavaScript
#134Earlier quoted context omitted.
I think the parent is talking about how C for instance does not have optional parameters (for better or worse), if this was the case for JS everyone who has ever used parseInt would be aware it takes a radix, but then again you also wouldn't be able to just plug it into map arbitrarily. Not saying one is better than the other, optional parameters can make for much less verbose code, I especially like parameter defaul…
C has function overloads, and you would have the same behavior you see here as you do in C.
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#135Earlier quoted context omitted.
The syntactic sugar here is that map and parseInt can be called without specifying all parameters. (But, since JS has no function overloading, it's the same function you're calling.)
Optional/defaulted function arguments are an extremely useful feature of many programming languages; are you arguing that they are always harmful?
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#136Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#137Earlier quoted context omitted.
As of ES5 010 must be parsed as 8 if you pass in a radix of 0.
Because leading 0 implies octal (Base-8). Interestingly, this is also the standard interpretation of IPv4 addresses in that form, where each delimited number [octet] in an IP address that start with a leading 0 should be read as octal. (That behavior contrasts interestingly with IPv6 where the numbers [segments] are only supposed to ever be hexadecimal (Base-16) and leading zeroes are to be ignored.)
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#138Recently I learned that in Javascript, [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.
[5+5]*2 is not NaN
Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript
#139Use Typescript. Regular JS just has too many gotchas to be usable in large applications. You can crank up the settings in TsLint and never worry about things like this again
https://www.typescriptlang.org/play/index.html#src=alert(%5B...
TSLint similarly reports nothing even with the tslint:all ruleset:
https://palantir.github.io/tslint-playground/?saved=N4Igxg9g... https://palantir.github.io/tslint-playground/