what i learned last week: the basics of execution context
Tags: javascript
Published: 2021-08-09 22:23:05

###Introduction

Welcome to this weeks entry of my WILLW series. Today, I'm going over the basics of execution context.

###What is the execution context

In JavaScript, [execution context](https://codeburst.io/js-demystified-04-execution-context-97dea52c8ac6) is an abstract concept that holds information about the environment within which the current code is being executed.

![execution context](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ds3rr95cu7f3t9uma55s.png)

###Types of execution context

There are three types of execution context and we are going to focus on two. 

- Global Execution Context
- Function Execution Context

First, [Global Execution Context](https://medium.com/@happymishra66/execution-context-in-javascript-319dd72e8e2c) is the default execution context in which JavaScript code start its execution when the file first loads in the browser.

![global execution context](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbro7m6dm8scisvxu55o.png) All of the global code i.e. code which is not inside any function or object is executed inside the global execution context. GEC cannot be more than one because only one global environment is possible for JavaScript code execution as the JavaScript engine is single threaded.

![call stack](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4r2k2s8zshxq7xp2hgy6.jpg)

Remember, a [call stack](https://dev.to/reyes2981/what-i-learned-last-week-under-the-hood-of-javascript-5e37) is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc. 

![javascript](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lw611h05eefh87o3y1qi.gif)

Next up, we have the [Function Execution Context](https://medium.com/@happymishra66/execution-context-in-javascript-319dd72e8e2c#:~:text=Functional%20execution%20context%20(FEC)%3A,has%20its%20own%20execution%20context) which is defined as the context created by the JavaScript engine whenever it finds any function call. [When](https://medium.com/@alexandrawilll/javascript-execution-context-call-stack-and-event-queue-d58b672d76f7#:~:text=When%20a%20function%20executes%2C%20an,off%20of%20the%20call%20stack.) a function executes, an execution context is created and virtually placed on top of the global execution context, the stacking of execution context is, again, called the call stack. Once a function has finished executing, it gets popped off of the call stack.

![Function Execution Context](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvuq0nb9yzliy825i5ab.png)

![execution context diagam](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiyseg7ck9mf9uohwb7w.png)

###The two phases of execution context creation

The creation of a execution context occurs in two phases
1. Creation
2. Execution

[The creation phase](https://www.javascripttutorial.net/javascript-execution-context/) 

When a script executes for the first time, the JavaScript engine creates a Global Execution Context. During this creation phase, it performs the following tasks:

Create a global object i.e., window in the web browser or global in Node.js.
Create a this object binding which points to the global object above.
Setup a memory heap for storing variables and function references.
Store the function declarations in the memory heap and variables within the global execution context with the initial values as undefined.

![global execution context](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4z16lpxrcbwymqv6tl10.png)

[The Execution Phase](https://www.javascripttutorial.net/javascript-execution-context/)

During the execution phase, the JavaScript engine executes the code line by line. In this phase, it assigns values to variables and executes the function calls.

![execution phase](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ajxdmq5l150260p14oqe.jpg)

That is it for this entry!