0% found this document useful (0 votes)
948 views9 pages

Untitled 2

The document explains the concept of Execution Context in JavaScript, which consists of a Memory Component for storing variables and functions, and a Code Component for executing code. It details the phases of code execution, including Memory Creation and Code Execution phases, as well as the role of the Call Stack in managing execution contexts. Additionally, it covers hoisting, local vs global access, the Window object, and the concept of scope in JavaScript.

Uploaded by

Anish Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
948 views9 pages

Untitled 2

The document explains the concept of Execution Context in JavaScript, which consists of a Memory Component for storing variables and functions, and a Code Component for executing code. It details the phases of code execution, including Memory Creation and Code Execution phases, as well as the role of the Call Stack in managing execution contexts. Additionally, it covers hoisting, local vs global access, the Window object, and the concept of scope in JavaScript.

Uploaded by

Anish Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
You are on page 1/ 9

JAVASCRIPT

TEXT

HOW JS WORKS & EXECUTION CONTEXT

A Everything in JavaScript happens in


“Execution Context”

This Execution Context has two parts Memory


Component( also known as Variable
Environment) and Code Component( also
known as Thread of execution).

Memory Component – Stores all the variables


and functions in code as key value pair.

A Code Component – Runs all the code line by


line

JavaScript is synchronous, Single-


Threaded
TEXT

HOW JS CODE IS EXECUTED?

When we start running code, Global


Execution Context is created. This creation
will happen in two phases

Memory Creation Phase and Code


Execution Phase

Memory Creation Phase – Memory is


allocated to all the variables and functions
in code. It stores variables with undefined
and functions with whole function in this
phase.

Now, code execution phase starts


TEXT

HOW JS CODE IS EXECUTED -


CONTINUED…

When code execution phase starts then the actual


values will be assigned in memory instead of
undefined.

When a function is called in program, then whole


new Execution Context will be created

Once the execution of function is completed the


newly created execution context will be deleted

Once the whole code is executed, global execution


context also will be deleted.

So, It can be very complex like function inside


function..(i.e context inside context ..) To manage all
these JS uses “CALL STACK”.

Call Stack maintains the order of execution of


execution contexts.
TEXT

HOISTING IN JAVASCRIPT
We can access variables and functions
before initialising without any errors this is
called hoisting.

But if we try to access any variable before


declaring we will get undefined. But incase
of functions it will print the entire function.

And also if I assign a arrow function to


variable it behaves like a variable, But If
we call it before initialising we will get
error.

Let’s see what causes this behaviour and


behind the scenes.
TEXT

HOISTING..
Recap, Before running the code memory
will be allocated(Memory creation phase),
for variables it will be undefined and for
functions it will be stored as function. So
from memory it will take it as undefined for
variables.

In case of arrow functions, It will be stored


as undefined in memory before code
execution, but if we call undefined(), so we
get error. Same for anonymous functions
as well.
TEXT

LOCAL VS GLOBAL ACCESS

Var x = 10

Function abd(){

Var x = 100;

Console.log(x) //it prints 100, it looks in


local execution context first.

}
TEXT

WINDOW OBJECT & THIS


When a program runs, this is when a
window object and this keyword will be
created.

Window is a global object. In browsers it is


called as window. In node it is different.

Even if file is empty this global object will


be created.

At global space, this === window (// true)

Global space is nothing but, anything not


inside a function is global space.
TEXT

LEXICAL ENVIRONMENT, SCOPE &


SCOPE CHAIN

Scope is access level of variable.

You might also like