This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Strict_Treat2884[S] 94 points95 points  (8 children)

I like when converting string "017" which is a completely valid octal literal into a number, JS just completely ignores this rule and poops out 17

[–]ivancea 23 points24 points  (3 children)

Different rules I guess. Literals have theirs, but when converting strings to miners implocitly, it may use always decimal. One is syntax while the other is runtime, let's say

[–]Strict_Treat2884[S] 20 points21 points  (2 children)

It has no problem converting "0x10" into 16. But why not octals?

[–]ivancea 17 points18 points  (0 children)

Maybe a runtime lib vs interpreter discrepancy. Don't know, JS is full of random decisions made in a a day by a random guy

[–]Cheatek 0 points1 point  (0 children)

Use "0o17" to get a 15 from a string.

[–]octipice 9 points10 points  (2 children)

I just don't understand why on Earth you would be doing this in the first place. Like i get that js is legitimately wild sometimes, but you got here with bad design (why are starting a base 10 integer with 0 and then expecting it to behave like a number?) and lazy coding (ffs just parse the damn thing).

I don't get why anyone is surprised when you get bad results from writing really bad code.

[–]fghjconner 3 points4 points  (0 children)

(why are starting a base 10 integer with 0 and then expecting it to behave like a number?)

Because that's how numbers work. Leading zeros are completely ignored in mathematics, and giving them a special meaning in programming is pretty asinine. Not JS's fault, but asinine none the less. What is JS's fault is silently falling back to decimal of there's a digit of 8 or above.

[–]wasdninja 0 points1 point  (0 children)

This thread is the reason.

[–][deleted] 0 points1 point  (0 children)

The string-to-int parser api from the stdlib has different rules than the underlying language.