Live data from Hacker News

Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript

medium.com

131–140 of 163 posts

Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript

#131
post #26

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…

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

#132
post #91

Earlier quoted context omitted.

If you want to get terse ['1', '7', '11'].map(x => +x)

This coerces to a number, but not to an integer.

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 JavaScript

#133
post #103

Earlier 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.

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 JavaScript

#134
post #26

Earlier 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.

You mean C++?

Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript

#135
post #40

Earlier 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?

Point me in my remark where I said that they are harmful. I was answering the question what the syntactic sugar was.

Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript

#136

Earlier quoted context omitted.

Well there's your first problem haha.

Right! It should have a map, map2, map3 function

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 JavaScript

#137

Earlier 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.)

(I'm trying to say that this behavior was removed in modern browsers. The only time when modern browsers are allowed to parse strings as a base other than base-10 when the second argument to parseInt is 0 or undefined is when the string starts with 0x or 0X, at which point the browser must parse it as base-16.)

Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript

#138
post #101
post #82

Recently 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

True, I was playing around to get examples in runJS. I think I mis pasted that one. Oh well.

Re: Why ['1', '7', '11'].map(parseInt) returns [1, NaN, 3] in JavaScript

#139

Use 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

Did you try testing that? The TypeScript compiler does't say a thing about that code:

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/

Post reply on HN