4 ms·
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 s
by plausibilities 7y ago
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]
- paulgb 7y agoTo someone reading the code, parseInt actually describes what your intent is while Math.round just happens to work because it coerces the type.
- bemmu 7y agoIf you want to get terse ['1', '7', '11'].map(x => +x)
- jacobolus 7y agoThis coerces to a number, but not to an integer.
- reaktivo 7y agoI would hate to see it in code that is being shared in a team, but the following works: ['1', '7', '11'].map(x => ~~x)
- plausibilities 7y agoIf I didn't care about rounding I'd just use a primitive constructor ['1', '7', '11'].map(Number)