SKU // 125

JS Screen

The screen object describes the display the browser is on: how many pixels it has, how much of it is free for windows, and how it is turned. It is easy to confuse with the window, which is only the part of the screen the browser occupies. This lesson shows what screen reports, why the numbers differ from the window, and what to use instead for responsive pages.

// Screen Versus Window

The screen is the whole display. The window is the browser on it. The viewport is the page area inside the window. Three different sizes, and they are rarely equal:

Property Measures
screen.width / screen.heightthe whole display, in pixels
screen.availWidth / screen.availHeightthe display minus permanent bars (taskbar, dock, menu bar)
window.outerWidththe browser window
window.innerWidththe page area inside the window

// Reading the Screen

console.log(screen.width > 0, screen.height > 0);
console.log(screen.availWidth <= screen.width);
console.log(screen.availHeight <= screen.height);
console.log(typeof screen.colorDepth, typeof screen.pixelDepth);
true true true true number number

Log screen.width and screen.height yourself to see your own display. colorDepth and pixelDepth are how many bits describe each pixel's color; 24 is typical and effectively the same number on modern devices.

// Orientation

screen.orientation tells you whether the device is wider than it is tall. Its type is one of four text values:

type Means
"landscape-primary"wide, the normal way up
"landscape-secondary"wide, turned upside down
"portrait-primary"tall, the normal way up
"portrait-secondary"tall, upside down
const types = ["landscape-primary", "landscape-secondary", "portrait-primary", "portrait-secondary"];
console.log(types.includes(screen.orientation.type));
console.log(typeof screen.orientation.angle);
true number

// Do Not Design With screen.width

It is tempting to read screen.width and choose a layout. Resist it. The screen size is not how much room your page has: the browser window can be smaller, split beside another app, or zoomed. What matters is the viewport. Two better tools:

// matchMedia: A Media Query in JavaScript

matchMedia takes a media query as text and returns an object whose matches is true or false right now. It can also tell you when the answer changes:

const wide = window.matchMedia("(min-width: 0px)");
console.log(typeof wide.matches, wide.matches);

const never = window.matchMedia("(min-width: 100000px)");
console.log(never.matches);
boolean true false
const query = window.matchMedia("(max-width: 600px)");

function update() {
  console.log(query.matches ? "small screen layout" : "large screen layout");
}

update();
query.addEventListener("change", update);

Media queries can ask about more than width: "(prefers-color-scheme: dark)" tells you if the person chose a dark theme, and "(prefers-reduced-motion: reduce)" tells you if they asked to avoid animation.

Privacy
Screen size, color depth, and similar details are some of the clues websites combine to recognize a device across visits ("fingerprinting"). Browsers are gradually limiting them, so another reason not to rely on exact screen values.

// Quick Check

Which one is the size of the page area inside the browser window?

  1. screen.width
  2. screen.availWidth
  3. window.outerWidth
  4. window.innerWidth
Show answer
D. innerWidth is the viewport. The screen values describe the display, and outerWidth is the whole browser window.

Which is always true?

console.log(screen.availWidth <= screen.width);
Show answer
true. The available width is the display minus permanent bars like a taskbar, so it can never be larger than the full width.

Why should you use a media query or matchMedia instead of screen.width to choose a layout?

Show answer
The screen size is not the space your page has. The window can be smaller than the screen, split, or zoomed. Media queries react to the real viewport.

// Try It

  1. Click Open Playground and run it. Write down your own screen and viewport sizes
  2. Change a number in the matchMedia query so the answer flips from true to false
  3. Ask matchMedia("(prefers-color-scheme: dark)") whether your device is in dark mode
  4. Turn your device sideways (or resize the window) and see which values change and which do not
// Video Walkthroughs
Last updated: Oct 11, 2026, 6:40 PM ET