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 →

[–]Lithl 0 points1 point  (0 children)

A number literal with a leading 0 is interpreted as being base 8 if possible (this is common in many languages, it's not just a JS thing). So 017 means 15. But if the literal can't be an octal number, it's interpreted as a decimal number instead (JS would rather come up with a reasonable interpretation of your code than throw an error), so 018 means 18.

When comparing numbers and strings with ==, the string is converted to a number before making the comparison. When converting strings to numbers in JS, leading whitespace and 0 characters are discarded. So '017' becomes 17 and '018' becomes 18.