How Javascript Works
How Javascript Works
By
Sivakumar G.R.
WHAT HAPPENS TO YOUR
CODE ?
• We must know how the JavaScript programs are executed internally.
• To understand core concepts like Hoisting, Scope and Closure, we must know how Execution
being executed.
Function EC
Eval EC Function EC 2
Function EC 1
Execution Context Stack — is a placed where our Global and Function Execution Context was
Execution phase
Lexical Environment
Creation Phase
In this phase two components are created
Variable Environment
EC has two components
Execution Context
Lexical Environment
Variable Environment
LEXICAL ENVIRONMENT
A lexical environment is a structure that holds identifier-variable mapping.
variable is the reference to actual object [including function object and array object] or
primitive value.
ER Type
Declarative ER Object ER
OUTER ENVIRONMENT & THIS
BINDING
Outer Environment
The reference to the outer environment means it has access to its outer lexical environment.
That means that the JavaScript engine can look for variables inside the outer environment if they are
not found in the current lexical environment.
this binding
the value of this is determined or set.
In the global execution context, the value of this refers to the global object. (in browsers, this refers
to the Window Object).
In the function execution context, the value of this depends on how the function is called.
If it is called by an object reference, then the value of this is set to that object, otherwise, the value
of this is set to the global object or undefined(in strict mode).
VARIABLE ENVIRONMENT
It’s also a Lexical Environment whose Environment Record holds bindings created by Variable
So it has all the properties and components of a lexical environment as defined above.
Environment Record
Outer Environment
‘this’ binding
Lexical Environment
Environment Record
Outer Environment
‘this’ binding
Variable Environment
OVERVIEW OF EXECUTION
CONTEXT
Environment Record
Outer Environment
‘this’ binding
Lexical Environment
Environment Record
Outer Environment
‘this’ binding
Variable Environment
Execution Context
OVERVIEW OF GLOBAL &
FUNCTION EC
Environment Record
Outer Environment
‘this’ binding
Lexical Environment
Environment Record
Outer Environment
‘this’ binding
Variable Environment
Environment Record
Outer Environment
‘this’ binding
Lexical Environment
Environment Record
Outer Environment
‘this’ binding
Variable Environment
Function EC
Global Execution Context
Lexical Environment
Environment Record
Outer Environment
‘this’ binding Variable Environment
Lexical Environment
getManagerDetails( ) EC
Environment Record
Outer Environment
Lexical Environment
‘this’ binding
Function EC onBoard( ) EC