Execution Contex

Execution Contex

Definition of an Execution Context

When you run a Java Script program, it creates an execution context. Memory creation and code execution take place within the Execution Context.

Execution context is like a container with two compartments.

  • Memory component

    This is the place where all variables and functions stored as a key: value pairs.

  • Code component

    This is the place where the code executes one line at a time.

HeadingImg.png

How the Execution Context works

It works in two phases

  • Memory Allocation Phase

    memory-allocation-img.png

    Java script will allocate memory to all variables and function.

    Variables will store undefined as a value inside the memory space.

    Functions will store whole code of the function inside the memory space.

  • Code Execution Phase

    The variable's undefined value will be replaced by the given value in the code. variable-value-assigining.png

    When it reaches line 8, where the fn function is called, a new execution context for the function is created.

    Then it will again go through the memory allocation phase, then code execution phase and will return the value to the variable addVal of the global execution context function-call.png

    After this the execution context for the function call will be deleted.

Summary

  • When you run JavaScript code, an Global execution context is created.

  • When a function is called, a local execution context is created inside the global execution context.

    • After completion of function execution, the local execution context will be removed.

    • After completion of the Java script program, the global execution context will also be removed.