# Execution Context

## Introduction

* When running a script, new execution context will be put into call stack
* To define an environment that the code is executed

## Scenario

```javascript
let x = 10;

function timesTen(a){
    return a * 10;
}

let y = timesTen(x);

console.log(y); // 100
```

## Global Execution Context

### Creation Phase



![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FpyEfXy3nYTMMr88OWm35%2Fimage.png?alt=media\&token=3a9e153f-4090-455a-94e8-87d3dc215578)

* Create the The Variable Object (VO) which is an object-like container created within an Execution Context
* For each variable declared with the `var` keyword, a property is added to VO that points to that variable and is set to 'undefined' (Hoisting)
* Create this : window
* Setup a memory heap for storing variables and function references

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FCK8ajkUUz70lWhWmVA6k%2Fimage.png?alt=media\&token=61a37987-2753-4789-89c1-dd8b87b60590)

### Execution Phase

* Execute functions line by line and create function execution context for each
* Assign variables with value

## Function Execution Context

### Creation Phase

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2Fo5v4YZCUBfywlwWUMAXL%2Fimage.png?alt=media\&token=75523d40-e4e6-4263-8f6a-05736561a1e0)

Create the argument object which refer to the param of function&#x20;

* Initialize the variable and param as undefined
* Create this: window

### ![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FIIDHjFU5t5lHhXraqvMM%2Fimage.png?alt=media\&token=8ddaef5f-e900-46e6-803d-0ce75f2e5476)

### Execution Phase

* Execute the code one by one
* Assign the value declared in the function

## References

{% embed url="https://www.youtube.com/watch?v=zdGfo6I1yrA" %}
