Screen Object in JavaScript

The Screen Object in JavaScript provides information about the user's screen, including its dimensions, color depth, and available width and height. This object is part of the Window interface and can be used to gather data related to the user's display, which can help create a responsive web experience.

Key Features of the Screen Object:

Common Properties of the Screen Object:

Code Example: Displaying Screen Properties

The following code example shows how to retrieve and display the properties of the screen using JavaScript:

Example


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");

Output

Screen Width: 1920px
Screen Height: 1080px
Available Width: 1900px
Available Height: 1060px
Color Depth: 24 bits
Pixel Depth: 24 bits

Explanation of Code:

Additional Code Example: Using Screen Object Properties for Responsive Design

This example adjusts the page content based on the user's screen width, providing a responsive experience:

Responsive Design Example


if (screen.width < 768) {
    document.body.style.fontSize = "14px";
} else if (screen.width < 1200) {
    document.body.style.fontSize = "16px";
} else {
    document.body.style.fontSize = "18px";
}

Output

The font size changes based on screen width.