JavaScript Screen
JavaScript Window Screen Information
Section titled “JavaScript Window Screen Information”The window.screen object provides information about the user’s screen, such as its dimensions and color depth.
Window Screen Object
Section titled “Window Screen Object”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 ascolorDepth.
Note: These properties reflect the screen capabilities, not necessarily the browser window size. For browser viewport size, use window.innerWidth and window.innerHeight.
Window Screen Width
Section titled “Window Screen Width”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");Window Screen Height
Section titled “Window Screen Height”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");Window Screen Available Width
Section titled “Window Screen Available Width”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");Window Screen Available Height
Section titled “Window Screen Available Height”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");Window Screen Color Depth
Section titled “Window Screen Color Depth”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). |
|---|
Window Screen Pixel Depth
Section titled “Window Screen Pixel Depth”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. |
|---|