SKU // 124

JS Window

The window object stands for the browser tab your page lives in. It holds the page itself, the address bar, the browser history, the screen, the timers, and every global variable and function you write. This lesson is the map of that object. The lessons after it zoom into its parts.

// The Global Object

In a browser, window is the global object: the top of everything. Things you have used since the first lesson, like console, setTimeout, and alert, are properties of it. So is the page, as window.document.

Property What it holds
window.documentthe page itself: its elements and content
window.locationthe current address (Location lesson)
window.historythe back and forward list (History lesson)
window.screenthe display (Screen lesson)
window.navigatorinformation about the browser (Navigator lesson)
window.localStoragesaved data (Web Storage lesson)
window.console, window.setTimeout...the tools you already use

// Everything Global Lives on window

A global var or function becomes a property of window. A global let or const does not (you saw this in JS Scope). Because window is the global object, you can leave it off: alert(1) and window.alert(1) are the same call.

var score = 10;
let level = 3;

console.log(window.score, window.level);
console.log(window === globalThis, window.window === window);
console.log(typeof window.document, typeof window.setTimeout, window.console === console);
10 undefined true true object function true
Do not collide with window
Every global you create is a property of the same object the browser uses for its own features. A global called name or status can silently clash with built-in window properties. Keep your variables inside functions or blocks, or use let and const.

// The Size of the Window

The page area you can draw in is the viewport. Its size in pixels is window.innerWidth and window.innerHeight. The whole browser window, including toolbars and borders, is window.outerWidth and window.outerHeight.

console.log(window.innerWidth > 0, window.innerHeight > 0);
console.log(typeof window.outerWidth, typeof window.outerHeight);
console.log(typeof window.devicePixelRatio);
true true number number number

The actual values depend on your device and window, so log them yourself to see them. devicePixelRatio says how many physical screen dots make up one CSS pixel: 1 on a basic display, 2 or more on a sharp one.

// Scrolling

window.scrollX and window.scrollY tell you how far the page has scrolled. window.scrollTo(x, y) jumps to a position and window.scrollBy(x, y) moves relative to where you are. Passing an options object with behavior: "smooth" animates it:

window.scrollTo(0, 0);
console.log(window.scrollX, window.scrollY);
console.log(window.pageYOffset === window.scrollY);
0 0 true
// back to the top, smoothly
window.scrollTo({ top: 0, behavior: "smooth" });

// Events on the Window

The window announces things that happen to the page as a whole. You listen with addEventListener:

Event Fires when
"resize"the window size changed
"scroll"the page was scrolled
"load"the page and everything it uses finished loading
"beforeunload"the user is about to leave (use sparingly)
"online" / "offline"the network connection came or went
<p id="out">Resize this window...</p>

<script>
  let count = 0;
  window.addEventListener("resize", function () {
    count++;
    document.getElementById("out").textContent =
      "Resized " + count + " times. Width: " + window.innerWidth;
  });
</script>

// Opening and Closing Windows

window.open(url) opens a new tab or window and returns a reference to it. window.close() closes one, but only a window your own code opened:

const popup = window.open("https://example.com", "_blank");
// later, from the same page:
popup.close();

Browsers block window.open unless it runs because the user did something, like a click. A plain link with target="_blank" is usually the better choice for sending someone to another page.

// Windows Inside Windows

A page can sit inside another page, in a frame. window.parent is the window that contains this one, and window.top is the outermost window. On an ordinary page, all three are the same window.

console.log(window === window.top);
console.log(window.parent === window.top);
false true

// globalThis

Not every JavaScript program runs in a browser. globalThis is the standard name for the global object everywhere: in a browser it is window, and in other places (like Node.js) it is something else. Prefer globalThis in code that might run outside a browser.

// Quick Check

Which statement is true about a global variable declared with var at the top of a script?

  1. It becomes a property of window
  2. It is hidden from window
  3. It is deleted when the page finishes loading
  4. It can only be read, not changed
Show answer
A. A global var (and a global function) becomes a property of window. A global let or const does not.

What does this log?

let a = 1;
var b = 2;
console.log(window.a, window.b);
Show answer
undefined 2. Only the var becomes a property of window.

What is the difference between window.innerWidth and window.outerWidth?

Show answer
innerWidth is the width of the page area (the viewport). outerWidth is the width of the whole browser window, including its toolbars and borders.

// Try It

  1. Click Open Playground and run it. Which of the logged values is different in your browser than in a classmate's?
  2. Declare a global var and a global let and show which one appears on window
  3. Log window.innerWidth, then change the size of the Result pane and run it again
  4. Use window.scrollTo to jump to the top, and write what window.scrollY is afterward
// Video Walkthroughs
Last updated: Oct 11, 2026, 6:31 PM ET