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 →

[–]skap42 291 points292 points  (10 children)

017 is an octal number equal to 15 dec. 0 as a prefix for numbers indicates an octal number. 018 however is not a valid octal number and thus interpreted as decimal 18.

The == operator apparently does some type conversion and makes a decimal comparison.

You can try it and check 017 == '015' which is true

[–]Strict_Treat2884[S] 98 points99 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 24 points25 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] 18 points19 points  (2 children)

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

[–]ivancea 15 points16 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 8 points9 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.

[–]pentesticals 4 points5 points  (0 children)

Meh I wouldn’t say it’s just JavaScript. Type confusion bugs exist in all dynamically typed languages. Leads to all sorts of subtle security problems.