3 ms·
If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead usin
by shawndumas 16y ago
If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt.
While the radix parameter is optional, always specify it to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified. [1]
----
[1]:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt#parameters https://developer.mozilla.org/en/JavaScript/Reference/Global...
- cracki 16y agothat's not a bug, that's a feature. a questionable feature, yes, but not a real bug.
- InclinedPlane 16y agoIt's a defect, it leads to inconsistent and unpredictable behavior. A better default behavior would have been to use a default radix (base 10) and have an option for auto-detected radix (e.g. strings that begin with 0x). Considering how rare octal formatted integer strings are these days (essentially non-existent) and how much more common 0 prefixed decimal formatted integer strings are, this is definitely not a useful feature.
- masklinn 16y ago> A better default behavior would have been to use a default radix (base 10) and have an option for auto-detected radix (e.g. strings that begin with 0x). Note that the option exists (provide `0` as radix). It's just stupid that the default radix is `0` (or `undefined`) and not `10`. Not to mention, that would put parseInt in line with Number.toString(radix), which (sensibly) defaults to base 10 (side note: I was surprised and impressed to see that Number.toString can handle any base from 2 to 36 included)
- cracki 16y agothat's not a bug, that's a feature. a questionable feature, yes, but not a real bug.
- antimatter15 16y agoBut CSS style attributes like .style.left are never zero-leading, and in that case you don't need to pass the radix.