This question comes up in almost every Frontend or Backend interview at the junior/middle level.
In short, an Execution Context is the environment in which our code is currently running; put simply, it is the value of this at the moment of execution. There are 3 types of execution context:
1. Global Execution Context
This context is created once, when the file is loaded, and it holds all the code that is not inside functions: global variables, global functions, and this at the top level.
In the browser, the global object is usually window.
2. Function Execution Context
The key thing to remember here: every function call creates a new context. Even if it is the same function, the context will be different each time. In it, JS stores the function parameters, local variables (ті, що всередині {}), and this for that specific call.
3. Module Execution Context
If the code runs as an ES module, it is executed in a module context. It is similar to the global one, but there are 2 important differences:
- Variables do not automatically become global
thisat the top level will beundefined