← Back to Chapters

JavaScript Screen Object

?️ JavaScript Screen Object

⚡ Quick Overview

The screen object in JavaScript provides information about the user's physical screen (monitor), such as its total width, height, and color depth. It is a property of the window object, so you can access it using window.screen or simply screen.

This is especially useful when you want to understand the user's screen resolution or calculate how much usable space is available for displaying your web page or popups.

? Key Concepts

  • The screen object is read-only and provides information about the physical screen.
  • It is different from the browser viewport; for viewport size, use window.innerWidth and window.innerHeight.
  • screen.availWidth and screen.availHeight exclude OS elements like taskbars.
  • screen.colorDepth and screen.pixelDepth tell you how many bits are used per pixel (often 24 or 32).
  • The screen object is available only in browser environments, not in Node.js.

? Syntax and Properties

Basic access to the screen object looks like this:

? View Basic Syntax
// Accessing the screen object
let s = screen;

// Common properties
s.width;        // Total screen width in pixels
s.height;       // Total screen height in pixels
s.availWidth;   // Usable width (minus taskbars/toolbars)
s.availHeight;  // Usable height (minus taskbars/toolbars)
s.colorDepth;   // Bits used per pixel (e.g., 24 or 32)
s.pixelDepth;   // Usually same as colorDepth

These properties help you adapt layouts, calculate popup sizes, or perform checks for media-heavy web applications where display capabilities (like color depth) matter.

? Summary of Screen Properties

Property Description
screen.width Total screen width in pixels.
screen.height Total screen height in pixels.
screen.availWidth Usable width excluding OS toolbars/taskbars.
screen.availHeight Usable height excluding OS toolbars/taskbars.
screen.colorDepth Bits per pixel (commonly 24 or 32).
screen.pixelDepth Usually the same as colorDepth.

? Code Examples

? View Code Example: Display Screen Properties
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Screen Properties</title>
</head>
<body>
  <h2>Screen Information</h2>
  <script>
    document.write("Screen Width: " + screen.width + "px<br>");
    document.write("Screen Height: " + screen.height + "px<br>");
    document.write("Available Width: " + screen.availWidth + "px<br>");
    document.write("Available Height: " + screen.availHeight + "px<br>");
    document.write("Color Depth: " + screen.colorDepth + " bits<br>");
    document.write("Pixel Depth: " + screen.pixelDepth + " bits");
  </script>
</body>
</html>
? View Code Example: Responsive Popup Size
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Responsive Popup</title>
</head>
<body>
  <button onclick="openPopup()">Open Centered Popup</button>

  <script>
    function openPopup() {
      let w = screen.availWidth / 2;
      let h = screen.availHeight / 2;

      // Calculate top-left position to roughly center the popup
      let left = (screen.availWidth - w) / 2;
      let top = (screen.availHeight - h) / 2;

      window.open(
        "https://example.com",
        "_blank",
        "width=" + w + ",height=" + h + ",left=" + left + ",top=" + top
      );
    }
  </script>
</body>
</html>

? Live Output

? See Your Screen Properties

Click the button below to display your current screen information using the screen object.

 

? Use Cases

  • Creating responsive popups sized according to the user's available screen space.
  • Adapting layouts for very small or very large screens.
  • Performing checks for graphics-intensive or media-heavy applications using color depth.
  • Providing analytics or logging about user screen resolutions.

? Tips and Best Practices

  • Prefer screen.availWidth and screen.availHeight when sizing popups to avoid overlapping OS taskbars.
  • Use window.innerWidth and window.innerHeight when you need the browser viewport size, not the full screen.
  • colorDepth and pixelDepth are useful for detecting display capabilities in media-heavy apps.
  • Remember that the screen object is read-only; you can read its properties but not modify the screen.
  • Always test on different devices and resolutions to handle varying DPI and scaling settings.
  • The screen object is available only in the browser environment, not in server-side JavaScript like Node.js.

? Try It Yourself

  • Open the browser console and run console.log(screen) to inspect all available properties.
  • Display the current screen width and height in an alert or directly on the page.
  • Center a popup window by calculating its position using screen.availWidth and screen.availHeight.
  • Write a script that shows a different message or layout if screen.width is below a certain breakpoint (e.g., 768px).