In this lesson, we will learn about window
. It is a global object provided by the browser.
It contains functions for opening tabs, controlling the position of the page, and various other things:
// It opens a new tab
window.open();
Windows are available in the DevTools console. Let's try calling the method from the example above there.
Here are some examples of its capabilities:
// It returns an object containing information about the screen
window.screen;
// Screen {availWidth: 1280, availHeight: 775, width: 1280, height: 800, …}
// It moves the page to a certain point (x-coord, y-coord)
window.scrollTo(0, 1000);
// It's visible page height and width
// They change when we resize the window
window.innerHeight;
window.innerWidth;
In addition, there's an object called document
inside window
. We can use in to work with the contents of the page in future lessons.
The window object sets the global execution context. The tag window
stores all other globally accessible properties and objects inside itself. When we call global functions such as alert()
or console.log()
, the browser looks for them in the window
object. In other words, we actually call window.alert()
.
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 danger of the global state
The presence of the window
object is a technical implementation from JavaScript, which should not be relied upon during development.
Let's observe 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, we have created a global variable.
Such variables cause a lot of problems during development. It's unclear where they come from and who changes them.
Global variables cannot be relied upon. There's a chance they might be modified by any part of the code, which often leads to errors in operation.
Moreover, on web pages, we can find various scripts which are not connected to each other. These can be various counters from analytical systems, marketing tools, and other such things. They all have access to the same window
.
Because of it, we can end up in this situation: we set properties in window
in one script and accidentally break the work of another script that uses the same global property.
If we want to achieve well-written code, we shouldn't directly encounter the window
object. However, knowing about its existence is important for understanding how JavaScript functions in browsers.
Do it yourself
- Open the console in your browser.
- Examine the
window
object. Try opening a new tab:
window.open();
Recommended materials
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
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.