← Back to JavaScript Course | Chapter 11: Browser Object Model | Lesson 2 of 6

JS Screen Object

window.screen is like checking the dimensions of the physical wall your window is mounted in, not the window itself, it tells you about the whole display, not just the browser's visible area.
Syntax
javascript
screen.width;
screen.height;
screen.availWidth;

What Is the Screen Object?

window.screen exposes information about the user's physical display device, independent of how large the browser window currently is. It's a read-only source of hardware/display facts, not something you configure.

उदाहरण: What Is the Screen Object?

javascript
console.log(screen.width, screen.height); // physical display resolution

Screen Size Properties

screen.width and screen.height report the full resolution of the display in pixels, while screen.availWidth and screen.availHeight subtract space taken by OS elements like the taskbar or dock.

The available values are usually the more useful ones for layout decisions.

उदाहरण: Screen Size Properties

javascript
console.log(screen.width, screen.height); // full resolution
console.log(screen.availWidth, screen.availHeight); // minus OS taskbar/dock

Color and Pixel Depth

screen.colorDepth and screen.pixelDepth report how many bits are used to represent color per pixel, almost always 24 or 30 on modern hardware.

This is rarely used today but matters for specialized graphics or accessibility tooling checking display capability.

उदाहरण: Color and Pixel Depth

javascript
console.log(screen.colorDepth, screen.pixelDepth);

Screen vs Window Dimensions

It's a common mix-up: screen.width is the monitor's total resolution, while window.innerWidth is the browser viewport size, which is almost always smaller.

Confusing the two leads to layout code that behaves correctly on a maximized window but breaks the moment the user resizes it.

उदाहरण: Screen vs Window Dimensions

javascript
console.log("Screen:", screen.width, "x", screen.height); // monitor resolution
console.log("Window:", window.innerWidth, "x", window.innerHeight); // browser viewport, usually smaller

Practical Uses

Screen properties are mostly used for adaptive experiences that care about the physical display, like choosing a higher-resolution image set for a high colorDepth screen, opening a popup sized relative to available screen space, or basic analytics about visitor display sizes.

उदाहरण: Practical Uses

javascript
// Check whether `screen.colorDepth >= 24`
// Check whether `screen.colorDepth >= 24`
if (screen.colorDepth >= 24) {
  // Print "Load high-resolution images" to the console
  // Print "Load high-resolution images" to the console
  console.log("Load high-resolution images");
// Otherwise, run this branch
// Otherwise, run this branch
} else {
  // Print "Load standard images" to the console
  // Print "Load standard images" to the console
  console.log("Load standard images");
}
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.