Reputation: 21
Reference: You don't know JS (scopes & closures)
Chapter 2: Lexical Scope
Consider an example:
var a = 2;
JavaScript first compiles the above code. So first the compiler does the lexical breakdown. It breaks it down as:
var a; &
a = ?;
While breaking it down as var a it informs the scope about it. And the scope maintains information that a is present within that scope or not.
Then begins the execution stage. Where a = 2 is assigned. I am trying to understand where the value of "a" i.e a = "2" stored before the execution stage starts i.e is there any memory allocation happening.
Upvotes: 0
Views: 105
Reputation: 665020
The compiler does not only do the lexical breakdown, it parses the whole code into an appropriate data structure (e.g. a parse tree) that also holds literals such as 2
. It basically holds an instruction such as "In a scope with the variable a
, assign the value derived from the constant expression 2
to the variable with the name a
."
Upvotes: 0