Javascript behaves differently with values having leading zeroes. alert(b) - prints different value.
var a = 67116;
var b = 00015;
alert(a);
alert(b);
As the other answers said, the leading zeroes make the number an octal literal. The decimal representation of the octal "15" is "13".
Note that there is no reason to use leading zeroes on number literals unless you really really want them to be interpreted as octals. I mean, don't use var b = 00015
. If you're getting that value from user input, then it will be a string (i.e. "00015"
), and you can convert to a decimal number with parseInt
:
var b = "00015"; // or var b = document.getElementById('some_input').value
var numB = parseInt(b, 10); // 15
A leading 0
makes the value an octal literal, so the value you put will be interpreted as a base 8 integer.
In other words, 015
will be equivalent to parseInt('15', 8)
.
var b = 00015
is an octal number
see this question for solution