The Global Environment and the Global Object
Execution Context (Global)
Whenever code is run in JS, it’s run inside an execution context, the wrapper created by the JS engine (the program that parsing that’s checking your code). This base execution context is called the global execution context. This is the thing accessible to everything, everywhere in your code. This holds the Global object, which holds a special variable ‘this’, and a reference to its outer environment.
In the browser, ‘window’ is the global object. Sidenote: if you’re running node, there’s a different global object. In other words, if you go to a clear webpage, devoid of all code, and type “this”, you will see something like this appear:
So Global Object(this) = window.
”The JavaScript interpreter in a browser is implemented as a single thread. This means that only one thing can be run at a time.
The execution stack
JS ran in browsers is single threaded. This means that only one thing can be run at a time. All actions and events are queued up in what is called the execution stack. This is where everything lives.
*Image above by Greg Rakozy
Here is an abstract view of what the execution stack might look like provided by Daivid Shariff:
The global object is always held at the base of the execution stack. Things are constantly popped on and off the stack, depending on your code. Understanding execution context is important because it leads to a better understanding of scope in JS.