0% found this document useful (0 votes)
40 views3 pages

Additional Notes - Execution Context and Execution Stack-2686

There are two types of execution contexts in JavaScript: the global execution context and function execution contexts. The global execution context is created first and handles code outside of functions, while function execution contexts are created for code within functions. An execution stack stores execution contexts and follows a LIFO structure, pushing new function contexts to the top as functions are called and popping them off as functions complete execution.

Uploaded by

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

Additional Notes - Execution Context and Execution Stack-2686

There are two types of execution contexts in JavaScript: the global execution context and function execution contexts. The global execution context is created first and handles code outside of functions, while function execution contexts are created for code within functions. An execution stack stores execution contexts and follows a LIFO structure, pushing new function contexts to the top as functions are called and popping them off as functions complete execution.

Uploaded by

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

Execution Context and Execution Stack

Execution Context
● An Execution context is an environment where JavaScript code is executed.

There are two types of Execution context in JavaScript:-

1.) Global Execution Context (GEC):

● The Global Execution Context is the default execution Context where all
javascript code that is not inside of any function.
● Everything happens in a global execution context.in this phase, memory is
allocated to all the variables and functions.
● There can be only one global execution context in a program.

2.) Function execution context :

● A new execution context is created whenever a function call is encountered.


● There can be multiple function execution contexts.

Execution Stack:

● An execution stack is used to store all the execution contexts which are
created during the code execution.
● An execution stack is also known as a calling stack(LIFO structure) in other
programming languages.
● When the JavaScript engine first encounters JavaScript code, it creates a
global execution context at the bottom and all the subsequent function
invocation is pushed to the top of the stack.
● The JavaScript engine executes the function whose execution context is at
the top of the stack. When this function completes, its execution stack is
popped off from the stack.
E.g. What happens when you invoke a function:

● Now first of all Global Execution Context is going to be created.


● Then execution starts and the interpreter encounters a call to function a(), and
here a new execution context is created and pushed on top of Execution
Stack.
● So now the Execution Context for a() is Created interpreter will execute the
code inside a() line-by-line.
● Then the interpreter encounters a call to function b(), this creates another
Execution Context which is pushed on top of the Execution Stack.
● When b() finishes it will be popped off the stack then a() will finish & down to
Global Execution Context.

You might also like