I have numbers in javascript from 01(int) to 09(int) and I want add 1(int) to each one.
For example:
01 + 1 = 2
02 + 1 = 3
But
08 + 1 = 1
09 + 1 = 1
I know the solution. I’ve defined them as float type.
But I want to learn, what is the reason for this result?
Thank you.
Javascript, like other languages, may treat numbers beginning with
0as octal. That means only the digits 0 through 7 would be valid.What seems to be happening is that
08and09are being treated as octal but, because they have invalid characters, those characters are being silently ignored. So what you’re actually calculating is0 + 1.Alternatively, it may be that the entire
08is being ignored and having0substituted for it.The best way would be to try
028 + 1and see if you get3or1(or possibly even30if the interpretation is really weird).