Why do console.log(00);
and console.log(01);
print 0 & 1 in the browser console and not 00 & 01?
console.log(00); // prints 0;
console.log(01); // prints 1;
console.log(011); // prints 9;
console.log(0111); // prints 73;
Why do
|
||||
That is because JavaScript treats leading 0 as octal number and that is why you are getting the octal number(base 8). You could use parseInt with radix to eliminate these kind of issues. And the reason why console.log treats the input as octal is, by default console.log calls of And the valueOf method returns like below:
Reference:- http://javascript.info Number System table |
|||||||||
|
Evaluating When executing Parsing Even before evaluating, there is another factor at play: parsing. Parsing is the first step of interpreting the source code and it refers to analysing the characters in the source code to figure out what logical constructs (tokens) they refer to. The tricky bit here is that numbers can be written in a few ways:
All of these refer to the same 2 numbers - 1 and 15, but they are different graphical representations the same way you would consider Once the source code is parsed, the characters written in the source code are "replaced" by the actual number they represent. This means that even before the Javascript engine knows it has to do some printing the characters you have written are gone and what remains are the actual numbers you have referenced. Annex B While the main Javascript spec considers octal numbers only those starting with |
||||
|
leading zero in console making it to base of 8.
|
|||||
|
|
|||
|
When you write console.log(00), the argument 00 is interpreted as number and so 0 is printed. On the other hand if you write console.log("00") 00 is interpreted as a string and 00 is printed. |
|||||||||
|
00
? – Biffen 22 hours ago