3 ms·
or just this: ['10', '10', '10'].map(val => parseInt(val)) Leading zeroes can often be considered invalid input, and you may be able to safely assume that i
by benatkin 4y ago
or just this:
['10', '10', '10'].map(val => parseInt(val))
Leading zeroes can often be considered invalid input, and you may be able to safely assume that it has been dealt with already.
Also the parens around a single argument of an arrow function are a superstitious thing suggested by some well-known react developer a while back, I think.
- contextnavidad 4y agoI have my linting setup to force a base on `parseInt`, for the extra characters I do feel it's worth it for the safety. The extra parentheses is just habit from Prettier formatting :)
- benatkin 4y agohttps://github.com/standard/standard/issues/384 https://github.com/standard/standard/issues/384
- jakear 4y agoAlso, no reason to write extra characters just to prevent people from using hexadecimal, it's often convenient and it comes for free!
- mseepgood 4y agoOctal is sneakier than hexadecimal, especially if the values are from user input. ['07', '08', '09'].map(val => parseInt(val)) // => [7, 0, 0]
- benatkin 4y agowas ['07', '08', '09'].map(val => parseInt(val)) // => [7, 8, 9]
- jakear 4y agoHasn't been an issue in around a decade: https://developer.mozilla.org/en-US/docs/web/javascript/reference/global_objects/parseint#browser_compatibility https://developer.mozilla.org/en-US/docs/web/javascript/refe...
- deleted 4y ago[deleted]
- benatkin 4y agoPlus there's this if you really want to support old browsers: https://github.com/zloirock/core-js/blob/ce52fdc735c5c809c9e85b2072f92d41b5a3885a/packages/core-js/internals/number-parse-int.js https://github.com/zloirock/core-js/blob/ce52fdc735c5c809c9e...