8

Whenever I see a website on the browser an instance of javascript is running. And I can declare a global variable in the console (DevTools);

var a = 1234567890; 

This variable has been declared in global scope such that I can get the value of the variable like so;

> a 
1234567890

However, I can also do this;

> window.a 
1234567890 

Am I understanding it correctly that the window object is the object that contains all the global variables within the website instance on the browser? If so to what scope does the window object belong? This is confusing me a little bit;

> window 
Window {top: Window, window: Window, location: Location, external:, ...}  
> window.window 
Window {top: Window, window: Window, location: Location, external:, ...}  
> window.window.window 
Window {top: Window, window: Window, location: Location, external:, ...}  

Is the window object the ultimate global object and does that have an object called window that refers back to itself?

3
  • We can see that window is pointing to itself.
    – jacouh
    Nov 8, 2013 at 9:45
  • That "duplicate" question is not a duplicate. This question: "Is a global variable just a property on Window?" That question: "How do I list all global variables?" Jan 28, 2016 at 3:48
  • @SteveBennett, this question's title is "list of all global variables javascript". You can always edit the title if you like. Jan 28, 2016 at 5:12

3 Answers 3

15

Is the window object the ultimate global object and does that have an object called window that refers back to itself?

Yes, and yes. This, for instance, returns true:

window.window.window.window.window === window.window;

You can, if you are interested, get a list of all the properties of the window object (and hence all global variables) with Object.keys:

console.log(Object.keys(window));

Note, however, that if you are spending too much time thinking about global variables, there is probably a problem with the architecture of your code.

1
  • good answer. i was more conserned with the fundamental architecture about the web. i avoid globals whenever possible :) Nov 8, 2013 at 9:51
4

Yes, the window object is

The Global Object(§15.1 ES5 Specification)

The unique global object is created before control enters any execution context.

Unless otherwise specified, the standard built-in properties of the global object have attributes {[[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}.

The global object does not have a [[Construct]] internal property; it is not possible to use the global object as a constructor with the new operator.

The global object does not have a [[Call]] internal property; it is not possible to invoke the global object as a function.

The values of the [[Prototype]] and [[Class]] internal properties of the global object are implementation-dependent.

In addition to the properties defined in this specification the global object may have additional host defined properties. This may include a property whose value is the global object itself; for example, in the HTML document object model the window property of the global object is the global object itself.

1

All global variables become properties of the window object.

>>> window.somevar = 1;
1
>>> somevar
1

And all of the core JavaScript functions are methods of the window object.

2
  • What does "core JavaScript functions" mean? Is String.indexOf a core Javascript function? Nov 8, 2013 at 10:10
  • 1
    @lonesomeday No, this is method of objects,from core javascript functions i meant to say that the functions that are not the part of any object like parseFloat,parseInt,isNaN
    – Voonic
    Nov 8, 2013 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.