window
— is a global object provided by the browser. Windows (tabs) are managed in the browser through it. It contains functions for opening tabs, controlling the position of the page and various other things.
// Opens a new tab
window.open();
Window is available in the DevTools console, it's quite convenient to look at it there. Try calling the method from the example above there.
Here are some examples of its capabilities:
// Returns an object containing information about the screen
window.screen;
// Screen {availWidth: 1280, availHeight: 775, width: 1280, height: 800, …}
// Moves the page to a point (x-coord, y-coord)
window.scrollTo(0, 1000);
// Visible page height and width
// Change when the window is resized
window.innerHeight;
window.innerWidth;
In addition, there's an object called document
inside window
that we can use to work with the contents of the page in future lessons.
The window object sets the global execution context. window
stores all other globally accessible properties and objects inside itself. Every time we call global functions such as alert()
or console.log()
, the browser looks for them in the window
object. In other words window.alert()
is what is actually called. The same applies to all other functions used directly and without imports:
console.log('hey');
// window.console.log('hey');
Math.abs(5);
// window.Math.abs(5);
// You can even do this
close();
// instead of window.close()
The presence of the window
object is a technical implementation from JavaScript, which should not be relied upon during development. Imagine this kind of code:
window.globalProperty = 'Global variables are evil';
Setting a property in window
automatically makes this property accessible from anywhere in the browser code. In other words, this is how global variables are created. Such variables create a lot of problems during development. It's unclear where they come from and who changes them and how. Global variables cannot be relied upon. There's a chance they might be changed by any part of the code, which often leads to errors in operation.
Moreover, scripts that are not connected to each other in any way are almost always connected on webpages. These can be various counters from analytical systems, marketing tools, and other such things. They all have access to the same window
. This means that setting certain properties in window
in one script may accidentally break the work of another script that uses the same global property.
In well-written code, the window
object will never be directly encountered. However, knowing about its existence is important for understanding how JavaScript functions in browsers.
window
object.Try opening a new tab:
window.open();
The Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From a novice to a developer. Get a job or your money back!
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.