When in <script> tag we are in Global Scope. Basically, it’s like being inside the main Window object, which is instantiated when you arrive at a URL.

It’s similar to this idea:

// Create a constructor function
let x = function Window() { this.x = 1; }
// Instantiate the object
let window = new x();
// Output x
alert(window.x); // 1

However, there is a difference between this object and simply assigning variables with var. Basically this-binding and Variable Environment are part of the same scope { }

It’s a bit tricky to explain — as many things in JavaScript — because if you try this in strict mode (the preferred way) it won’t even work in the same way. But most people learning JavaScript don’t learn variable definitions in strict mode. Hence layers of confusion. At the end of the day… keep experimenting and absorbing… eventually it’ll start making sense.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store