The Global Environment and the Global Object

Whenever code is run, it is run inside an execution context.

Execution context being first created, then run.

For example, the global execution context creates 2 things for you:

– a global object
– this variable

Then when you run it, the ‘this’ will point to the global object for you to reference. Anything you implement will also be attached to this global object.

Let’s see how it works:

1) create an empty html page
2) create an empty JS file
3) include this empty JS file in the html page

Now run the html page. Look in Inspect, and under Console tab, type this

You’ll see the Window object.

The execution context was created by the JS engine. And it decides the value of what ‘this’ be. In the currently situation, this is pointing to the window object.

If you’re running JS in Node, you’ll get a different object and ‘this’ will be pointing to that instead.

An execution context was created at the global level as a global object. ‘this’ refers to the window object.

Global simply means ‘not instead a Function’

Your definitions being attached to the global object

The execution context is created

the ‘this’ reference and ‘window’ object are available.

Anything you create globally (the variable ‘a’ and function ‘b’) will be attached to the global object:

console.log(window.a); // Hello World
window.b() // test

When code is executed, an execution context is created.

At the base level, you have a Global Object.
If you are in the browser, that global object is ‘window’ object.

You get a special variable called ‘this’, which references the global object.