javascript
  1. javascript-screen-object

JavaScript Screen Object

The Screen object in JavaScript returns information about the user's screen, such as width and height.

Syntax

screen.property

Example

var screenWidth = screen.width;
var screenHeight = screen.height;

console.log("Screen Width: " + screenWidth);
console.log("Screen Height: " + screenHeight);

Output

The above code will output the user's screen width and height in pixels.

Screen Width: 1920
Screen Height: 1080

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Screen Information</title>
</head>
<body>

<!-- Display screen information -->
<p id="screenWidth"></p>
<p id="screenHeight"></p>

<!-- Your JavaScript code goes here -->
<script>
  // Get the screen width and height
  var screenWidth = screen.width;
  var screenHeight = screen.height;

  // Display the screen information in HTML
  document.getElementById('screenWidth').innerText = 'Screen Width: ' + screenWidth + ' pixels';
  document.getElementById('screenHeight').innerText = 'Screen Height: ' + screenHeight + ' pixels';
</script>

</body>
</html>
Try Playground

Explanation

The Screen object in JavaScript provides information about the user's screen. There are a number of properties that can be accessed, such as width, height, colorDepth, and pixelDepth.

Use

The Screen object is commonly used to determine the screen dimensions of the user's screen. This can be helpful when designing web pages or applications that need to take into account the available screen real estate.

Important Points

  • The Screen object provides information about the user's screen.
  • The width and height properties return the size of the screen in pixels.
  • Other properties, such as colorDepth and pixelDepth, provide additional information about the user's screen.

Summary

The Screen object in JavaScript is a useful tool for obtaining information about the user's screen. It provides access to important details such as screen dimensions, color depth, and pixel depth. This information can be used to design web pages and applications that are optimized for the user's screen.

Published on: