Javascript behaves differently with values having leading zeroes. alert(b) - prints different value.
var a = 67116;
var b = 00015;
alert(a);
alert(b);
I am more interested to know What conversion is applied here by javascript inside alert(b) ? (If i have them in double quotes. They work fine.)
To pad an integer with leading zeros to a specific length To display the integer as a decimal value, call its ToString(String) method, and pass the string "Dn" as the value of the format parameter, where n represents the minimum length of the string.
To remove the leading zeros from a number, call the parseInt() function, passing it the number and 10 as parameters, e.g. parseInt(num, 10) . The parseInt function parses a string argument and returns a number with the leading zeros removed.
Meaning of number pad in Englisha display of numbers on a mobile phone or electrical device, that you press to make the device do something: The keyboard is hidden behind the phone's number pad.
In JavaScript, to pad a number with leading zeros, we can use the padStart() method. The padStart() method pads the current string with another string until the resulting string reaches the given length. The padding is applied from the start of the current string.
var b = 00015
is an octal number
see this question for solution
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With