01. Execution Context

When JS code executes, it always happens within certain execution context. For each of types of executable code, there’s an execution context. There are 4 types of executable code in JS:

  • Global code,

    • Content of <script /> elements

    • Non-module code

  • Module code,

    • Content of <script type="module" />

    • Module code

  • Function code,

    • Content of event attributes (e.g. <p onclick="...">)

    • Anything that’s executed directly within a function,

  • Eval code.

    • Text that’s supplied to a built-in eval function is parsed as Eval code

As you can see, execution contexts can logically form a stack:

  1. First there might be Global code or Module code with its own execution context;

  2. That code might call a function, with its own execution context;

  3. That function could call another function, and so on and so forth.

  4. Even if function is calling itself recursively, a new execution context is being entered with every invocation.

Last updated