Course contentsShow
← JavaScript
Lesson 1474 of 2,72332. JavaScript in the BrowserFree lesson

The Screen Object

Reading screen dimensions and properties with window.screen for responsive behavior.

The Screen Object

You'll learn how to read display dimensions and properties using window.screen to make your JavaScript responsive to different screen sizes.

What Is the Screen Object?

The window.screen object provides information about the user's physical display—their monitor or device screen. Unlike the browser window (which can be resized), the screen object tells you about the actual hardware display behind the browser.

Think of it like the difference between a picture frame (the browser window) and the wall it hangs on (the screen). The wall doesn't change size when you adjust the frame.

Accessing Screen Properties

The screen object offers several read-only properties:

// Get the full screen dimensions
console.log(screen.width);        // e.g., 1920 (total pixels wide)
console.log(screen.height);       // e.g., 1080 (total pixels tall)

// Get the available space (excluding taskbars, docks, etc.)
console.log(screen.availWidth);   // e.g., 1920
console.log(screen.availHeight);  // e.g., 1040

// Check color depth
console.log(screen.colorDepth);   // e.g., 24 (bits per pixel)

// Get pixel ratio for high-DPI displays
console.log(screen.pixelDepth);   // Usually same as colorDepth

Practical Use Case

You might use screen dimensions to decide whether to show a mobile or desktop layout:

if (screen.width < 768) {
  console.log('Small screen detected - loading mobile assets');
} else {
  console.log('Large screen detected - loading desktop assets');
}

Note that screen gives you the display size, while window.innerWidth gives you the browser window size. They're often different!

Key Takeaway: The window.screen object reveals your user's physical display dimensions and properties, helping you create responsive experiences that adapt to different hardware setups.