المعرفة:: JavaScript الحالة::مؤرشفة المراجع:: syahshiimi’s notes, The Complete JavaScript Course 2022 From Zero to Expert


How Is JavaScript Code Executed?

Top Level Code

Top level codes are not inside a function which are through the creation of global-execution context.

const name = "Person";

Execution Context: An environment where a piece of JavaScript code is executed and where it stores all the necessary information.

Execution of top-level code

There can only be one global execution context.

Execution of functions and waiting for callbacks

  • The engine will execute on execution context per function. For each function, a new execution context is created.
  • These contexts together make the call stack.

Execution Context (In Detail)

What is inside an EC?

  1. Variable environment such as var, let or const.
  2. Functions
  3. Arguments object
  4. Scope Chain: references to variables that are located outside of the current function.
  5. this keyboard

EC in Arrow functions

In arrow functions, the EC won’t have arguments object nor this keyword. Instead, they can use the arguments object and this from their closest regular function parent.

How Does The Function Know Where It Is?

  • We can understand this through the engine’s call stack.
  • The call stack is a place where the execution context is set on top of each other to keep track of where we are.
  • The EC at the top of the call stack will be the one that is running.