4 ms·
JSON was made to be "based on a subset" of Javascript. The only way to be compatible with JS while removing octals is to disallow leading zeroes entirely. Doing
by ChrisSD 7y ago
JSON was made to be "based on a subset" of Javascript. The only way to be compatible with JS while removing octals is to disallow leading zeroes entirely. Doing otherwise would lead to JSON and JS behaving differently with the same input.
Of course, until recently JSON wasn't a strict subset of JS but that was an oversight rather than by design.
- magicalhippo 7y agoI've been programming for 30 years, across many different languages from assembler and up. I've yet to use octals for any code. What am I missing out on? Why are they included in modern languages like JS?
- aliceryhl 7y agoI once used octals in php: I had to specify the permissions for some file, and those should be in octal. I assume JS has it because so does everyone else.
- userbinator 7y agoAlmost certainly due to C heritage.
- peterwwillis 7y agoIf you ever want to count in base-8 (or a subset), or have an abbreviated form of binary, or pack decimal in a way that's easier to reason, or divide a number in half (down to 1) without getting fractions, or represent file permissions (a grouping of four octals). Is JS a modern language? It was made 24 years ago as a prototype for a scripting language loosely mimicing Java. Presumably octals would have been still used often on recent machines.
- magicalhippo 7y ago> If you ever want to count in base-8 (or a subset) So far I haven't ever had that need it seems. > have an abbreviated form of binary Yeah ok, but why octal over hex? After all, hex maps better to the underlying storage. > pack decimal in a way that's easier to reason How'd that work? I know about BCD but I don't see how octal improves the situation, being base-8. > divide a number in half (down to 1) without getting fractions Huh? > represent file permissions (a grouping of four octals) Ok, I get that for C and such, but how often do you do that in JS? > Is JS a modern language? Compared to C, where octal support is understandable, I'd say yes.
- lilyball 7y agoNot only that, but rejecting leading zeroes allows parsers to add octal support themselves by accepting the leading zero, just as how some JSON parsers extend JSON by allowing comments, or trailing commas, or NaN/+Inf/-Inf.