-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
The JavaScript Screen Object is part of the Browser Object Model (BOM) and provides information about the user’s display screen — such as width, height, color depth, and pixel depth.
This information helps developers make responsive designs, adjust layout dynamically, and even detect screen capabilities for optimization (for example, choosing the right image quality for different devices).
The screen
object represents the display monitor or screen on which the browser window is being viewed.
It’s a property of the window object, meaning you can access it either as window.screen
or just screen
.
<script>
// Both are valid
console.log(window.screen);
console.log(screen);
</script>
The screen
object helps you:
Detect the user’s screen size (useful for responsive designs).
Get information about color depth (important for image quality).
Understand available screen area after excluding taskbars and toolbars.
Make decisions based on screen resolution, like adjusting layout or image sizes.
Here are the most frequently used properties of the screen
object:
Property | Description |
---|---|
screen.width |
Returns the total width (in pixels) of the user’s screen. |
screen.height |
Returns the total height (in pixels) of the screen. |
screen.availWidth |
Returns the width of the screen available to the browser (excluding OS taskbars). |
screen.availHeight |
Returns the height of the available screen area (excluding the taskbar). |
screen.colorDepth |
Returns the number of bits used to display one color (usually 24). |
screen.pixelDepth |
Returns the color resolution of the screen in bits per pixel (often the same as colorDepth ). |
Here’s a simple example that displays all screen details on the webpage.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Screen Object Example</h2>
<p id="info"></p>
<script>
// Get screen information
let info = `
Screen Width: ${screen.width}px <br>
Screen Height: ${screen.height}px <br>
Available Width: ${screen.availWidth}px <br>
Available Height: ${screen.availHeight}px <br>
Color Depth: ${screen.colorDepth} bits <br>
Pixel Depth: ${screen.pixelDepth} bits
`;
// Display on the page
document.getElementById("info").innerHTML = info;
</script>
</body>
</html>
Explanation:
screen.width
and screen.height
give the full resolution.
availWidth
and availHeight
exclude OS UI areas (like taskbars).
colorDepth
tells how many bits represent a single pixel’s color (e.g., 24 bits = true color).
Property | Includes Taskbar? | Example Use |
---|---|---|
screen.width , screen.height |
✅ Yes | For total display resolution |
screen.availWidth , screen.availHeight |
❌ No | For browser window space available |
This distinction is helpful when you want to create popups or responsive layouts that fit perfectly in the user’s visible area.
You can use screen.width
to adjust your UI or behavior for different devices.
<script>
// Detect screen width and show a message
if (screen.width < 600) {
alert("You are using a mobile device!");
} else if (screen.width < 1200) {
alert("You are on a tablet or small laptop.");
} else {
alert("You are on a desktop screen.");
}
</script>
Explanation:
This helps you understand what type of device the user might be on, allowing you to optimize your layout or functionality.
You can use screen dimensions to open a new popup exactly in the center of the user’s screen.
<script>
// Function to open a centered popup
function openCenteredPopup() {
const popupWidth = 400;
const popupHeight = 300;
// Calculate center position
const left = (screen.width - popupWidth) / 2;
const top = (screen.height - popupHeight) / 2;
// Open the popup
window.open(
"https://www.example.com",
"_blank",
`width=${popupWidth},height=${popupHeight},left=${left},top=${top}`
);
}
</script>
<button onclick="openCenteredPopup()">Open Centered Popup</button>
Explanation:
The code calculates the left
and top
positions using screen dimensions.
window.open()
then opens the new window perfectly centered.
Both colorDepth
and pixelDepth
describe how many bits represent color per pixel.
For most modern devices:
colorDepth = 24 bits
→ True color (16 million colors)
pixelDepth = 24 bits
→ Usually the same as color depth
<script>
document.write("Color Depth: " + screen.colorDepth + " bits<br>");
document.write("Pixel Depth: " + screen.pixelDepth + " bits");
</script>
These properties are mostly informational now, as modern screens almost always support 24-bit color.
You can use the screen object to check if the user is using a low-resolution display and adjust the website’s visuals accordingly.
<script>
if (screen.width < 1024 || screen.colorDepth < 24) {
document.body.style.backgroundColor = "#fdd";
document.write("Low-resolution or limited color display detected.");
}
</script>
Remember, screen
is actually a property of the global window
object.
So these are equivalent:
console.log(screen.width);
console.log(window.screen.width);
That means all BOM-related objects like location
, history
, navigator
, and screen
are accessible under window
.
It doesn’t give the browser window size — use window.innerWidth
for that.
You can’t control or resize the user’s screen (for security reasons).
The values might differ slightly across devices and browsers.
It only provides read-only information.
The JavaScript Screen Object gives you important details about the user’s screen — width, height, available space, and color capabilities.
You can use it to:
Detect device type (mobile, tablet, desktop)
Adjust layouts or popup positioning
Monitor screen resolutions for analytics
Optimize content for color and display features
How can you display the total width and height of the user’s screen using the screen
object?
Write code to display the available screen width and height (excluding taskbars) on the webpage.
How can you use the screen object to check if a user is on a mobile device (screen width < 600px)?
Write a script to alert the user if their screen color depth is less than 24 bits.
How can you access the screen’s pixel depth and display it in a <p>
element?
Write code to open a popup window centered on the user’s screen using screen.width
and screen.height
.
How can you log all properties of the screen
object to the console for debugging purposes?
Write a program to change the background color of the page if the available screen width is smaller than 1024 pixels.
How can you compare screen.width
and window.innerWidth
to detect the difference between full screen and browser viewport?
Create a function that checks if the user’s screen resolution is high definition (width ≥ 1920 and height ≥ 1080) and displays a corresponding message.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts