Scope Flashcards
Explain the steps javascript takes to execute its code?
- Parsing Phase
- Syntax checking happens here
- Creation Phase
- As it goes through the code, Whenever it encounters syntax that needs new scope like blocks, functions, module etc, it creates new scope (called Environment Record in ECMAScript spec).
- As it evaluates code in that particular scope, it adds identifier bindings to particular environment record and allocates memory.
- For variables, only identifier bindings and memory allocation happens but values wont get assigned.
- For functions, function objects get created and stored in memory in this phase itself.
- Execution Phase
- In this phase, assignment happens, identifier values get retrieved and functions get called, etc.
How does var, let & const are evaluated differently in parsing phase?
- var
- Does nothing if duplicate identifier are present
- let & const
- Throws syntax error
Initially, i was thinking this check will be done in creation phase, but javascript throws syntax error which tells this check is being done in parsing phase. Below URL contains early errors concept mentioned in spec for let and const.
https://tc39.es/ecma262/#sec-let-and-const-declarations-static-semantics-early-errors
How does javascript enforce const initialisation? Is it done during execution phase, creation phase or parsing phase? Give reasons why it doesnt happen in particular phase.
Execution Phase: It doesnt happen in execution phase because if i have a console.log before declaring const without initialising, log wont get executed. It tells that this check is done before execution phase.
Creation Phase: It very well could have been considered to be checked during creation phase. But the error that is thrown by javascript says “SyntaxError” and spec says that “Syntax Error’ is thrown as an early error before it starts evaluating code.
Therefore, answer is parsing phase.
https://tc39.es/ecma262/#sec-let-and-const-declarations-static-semantics-early-errors
Quote
Whenever javascript throws any error, consider which error is being thrown and in which scenarios it throws those errors.
What happens when functions are evaluated in creation phase? Do the code inside function also get evaluated and identifier bindings get created in this initial creation phase?
When functions are encountered during creation phase, function objects get created and stored in memory. But inner code of function will not get evaluated until that function gets called. When any function gets called, new execution context gets created and for that execution context, creation and execution phases will run.
Proof: This can be verified using memory dev tools probably (Not Verified Yet)
Quote
Creation and Execution Phases are different for each execution context inside a script or module but parsing phase is done for whole script or module only once.
Proof:
Executing this code will not log “start”. That is the proof that parsing phase is same for function and module or script.
console.log(“start”);
function func() {
const a = 1;
console.log(a);
var a = 2;
}
func();