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
source share