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.
screen object is read-only and provides information about the physical screen.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).screen object is available only in browser environments, not in Node.js.Basic access to the screen object looks like this:
// 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.
| 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. |
<!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>
<!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>
Click the button below to display your current screen information using the screen object.
screen.availWidth and screen.availHeight when sizing popups to avoid overlapping OS taskbars.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.screen object is read-only; you can read its properties but not modify the screen.screen object is available only in the browser environment, not in server-side JavaScript like Node.js.console.log(screen) to inspect all available properties.screen.availWidth and screen.availHeight.screen.width is below a certain breakpoint (e.g., 768px).