Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript variable with leading zeroes

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.)

like image 805
Kris Avatar asked Jun 14 '13 17:06

Kris


People also ask

How can I pad a value with leading zeros?

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.

How do I remove leading zeros in typescript?

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.

What is a pad number?

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.

How do you number a pad in JavaScript?

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.


2 Answers

var b = 00015

is an octal number

see this question for solution

like image 153
Jonathan DS Avatar answered Nov 02 '22 07:11

Jonathan DS


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).

like image 36
Andrew Clark Avatar answered Nov 02 '22 08:11

Andrew Clark