Register to get access to free programming courses with interactive exercises

Global Window Object JS: DOM API

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.

Window

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 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. 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.


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

Hexlet Experts

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

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Profession
beginner
Development of front-end components for web applications
start anytime 10 months

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.