Register to get access to free programming courses with interactive exercises

Global Window Object JS: DOM API

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.

Window

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

  1. Open the console in your browser.
  2. Examine the window object.
  3. Try opening a new tab:

    window.open();
    

Recommended materials

  1. Window

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

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.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs
profession
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

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.