Skip to content

JavaScript Screen

The window.screen object provides information about the user’s screen, such as its dimensions and color depth.

The window.screen object can be accessed directly as screen (since window is the global object in browsers).

Key Properties:

  • screen.width: Total width of the user’s screen in pixels.
  • screen.height: Total height of the user’s screen in pixels.
  • screen.availWidth: Width of the screen available for application windows, excluding interface features like the OS taskbar.
  • screen.availHeight: Height of the screen available for application windows, excluding interface features like the OS taskbar.
  • screen.colorDepth: Number of bits used to display one color (also known as bit depth).
  • screen.pixelDepth: Bit depth of the screen. For modern systems, this is usually the same as colorDepth.

Note: These properties reflect the screen capabilities, not necessarily the browser window size. For browser viewport size, use window.innerWidth and window.innerHeight.

The screen.width property returns the total width of the user’s screen in pixels.

// Assuming an HTML element with id="demo"
// const demoElement = document.getElementById("demo");
// if (demoElement) demoElement.textContent = "Screen Width: " + screen.width + "px";
console.log("Screen Width: " + screen.width + "px");

The screen.height property returns the total height of the user’s screen in pixels.

// const demoElement = document.getElementById("demo");
// if (demoElement) demoElement.textContent = "Screen Height: " + screen.height + "px";
console.log("Screen Height: " + screen.height + "px");

The screen.availWidth property returns the width of the screen available for windows, in pixels, excluding OS interface features like a fixed taskbar or menu bar.

// const demoElement = document.getElementById("demo");
// if (demoElement) demoElement.textContent = "Available Screen Width: " + screen.availWidth + "px";
console.log("Available Screen Width: " + screen.availWidth + "px");

The screen.availHeight property returns the height of the screen available for windows, in pixels, excluding OS interface features.

// const demoElement = document.getElementById("demo");
// if (demoElement) demoElement.textContent = "Available Screen Height: " + screen.availHeight + "px";
console.log("Available Screen Height: " + screen.availHeight + "px");

The screen.colorDepth property returns the number of bits used to represent the color of a single pixel.

Common values for modern displays:

  • 24 bits: Represents 16,777,216 different colors (“True Color”).
  • 30 bits / 32 bits (often 24-bit color with an 8-bit alpha channel): Can represent over a billion colors (“Deep Color”), though display support varies.

Older systems might have used 16-bit (65,536 colors, “High Color”) or 8-bit (256 colors).

// const demoElement = document.getElementById("demo");
// if (demoElement) demoElement.textContent = "Screen Color Depth: " + screen.colorDepth + "-bit";
console.log("Screen Color Depth: " + screen.colorDepth + "-bit");
CSS color values like #RRGGBB or rgb(r,g,b) typically define colors within a 24-bit color space (8 bits per channel).

The screen.pixelDepth property also returns the color bit depth of the screen.

// const demoElement = document.getElementById("demo");
// if (demoElement) demoElement.textContent = "Screen Pixel Depth: " + screen.pixelDepth + "-bit";
console.log("Screen Pixel Depth: " + screen.pixelDepth + "-bit");
For modern computers, screen.colorDepth and screen.pixelDepth generally return the same value.