Console.log output in javascript

Why console.log(00); and console.log(01); print 0 and 1 in browser console, not 00 and 01?

 console.log(00); // prints 0; console.log(01); // prints 1; console.log(011); // prints 9; console.log(0111); // prints 73; 
+6
source share
5 answers

Never write a number with a leading zero (e.g. 07). Some versions of JavaScript interpret numbers as octal if they are written with a leading zero.

This is because JavaScript treats the leading 0 as an octal number, and that is why you get the octal number (base 8).

You can use parseInt with the foundation to fix such problems.

And the reason console.log treats input as octal is that the console.log valueOf calls the input method by default. If it returns nothing, it calls the toString method.

And the valueOf method is returned as shown below:

 00.valueOf() //0 01.valueOf() //1 011.valueOf() //9 0111.valueOf() //73 

Link: - http://javascript.info

System table number

Number system

+21
source

Rating

When executing console.log(stuff) before printing, stuff is evaluated to see what needs to be printed. This is why console.log(3+2) prints 5 , not 3+2 . This has nothing to do with console.log , in particular, evaluation is performed before console.log executed. Any function will behave identically, it will be passed in with an estimated 5 as a parameter, not the initial 3+2

Syntactic

Even before evaluation, there is another factor in the game: parsing. Analysis is the first step in interpreting the source code, and it refers to the analysis of characters in the source code to find out which logical constructs (tokens) they refer to.

The hard bit here is that numbers can be written in several ways :

  • 1 , 15 - numbers written in decimal form (base 10)
  • 01 , 017 - numbers written in octal (base 8)
  • 0b1 , 0b1111 - numbers written in binary form (base 2)
  • 0x1 , 0xE - numbers written in hexadecimal (base 16)

They all refer to the same two numbers - 1 and 15, but they represent different graphical representations just as you would consider 6 and 0000006 same number.

After analyzing the source code, the characters recorded in the source code are β€œreplaced” by the actual number that they represent. This means that even before the Javascript engine knows that it needs to do some printing, the lost characters have disappeared, and the remains are the actual numbers you referenced.

Appendix B

While the main Javascript spec only considers octal numbers that start with 0o , obsolete syntax

+11
source

leading zero in the console, based on 8.

 console.log(00)=0 console.log(01)=1 console.log(011)=9 //0+8+1 console.log(0111)=73 //0+64+8+1 
+6
source

When you write console.log (00), the argument 00 is interpreted as a number and therefore prints 0. On the other hand, if you write console.log ("00") 00 is interpreted as a string and 00 is printed.

+1
source

The reason console.log is always displayed in DEC format. For example: 011 is 11 in OCT and 9 in DEC. enter image description here

+1
source

Source: https://habr.com/ru/post/1015427/


All Articles