Screen Object
In this tutorial, we will learn about the Screen object in JavaScript.
In JavaScript, the screen object provides information about the screen size and resolution of the device on which the browser is running. It is a global object that is automatically created by the browser and is available to all JavaScript code running in the browser.
Accessing the Screen Object
You can access the screen object in JavaScript using the window.screen property. For example, you can use the screen object to get information about the screen size and resolution:
// Get the width of the screen
let screenWidth = window.screen.width;
console.log(screenWidth);
// Get the height of the screen
let screenHeight = window.screen.height;
console.log(screenHeight);
// Get the color depth of the screen
let screenColorDepth = window.screen.colorDepth;
console.log(screenColorDepth);These properties allow you to get information about the screen, such as its width, height, and color depth.
Properties of the Screen Object
The screen object has several properties that provide information about the screen size and resolution. Some of the commonly used properties are:
screen.width: Returns the width of the screen in pixels.screen.height: Returns the height of the screen in pixels.screen.availWidth: Returns the available width of the screen in pixels.
You can access these properties using the screen object. For example, to get the width of the screen, you can use window.screen.width.
Methods of the Screen Object
The screen object also has several methods that allow you to interact with the screen. Some of the commonly used methods are:
screen.lockOrientation(): Locks the orientation of the screen.screen.unlockOrientation(): Unlocks the orientation of the screen.
You can call these methods using the screen object. For example, to lock the orientation of the screen, you can use window.screen.lockOrientation().
Examples
Here are some examples of using the screen object:
// Lock the orientation of the screen to landscape
window.screen.lockOrientation("landscape");
// Unlock the orientation of the screen
window.screen.unlockOrientation();These examples demonstrate how to lock and unlock the orientation of the screen using the screen object. You can use these methods to control the orientation of the screen based on your application's requirements.
Conclusion
- The
screenobject provides information about the screen size and resolution of the device on which the browser is running. - You can access the
screenobject using thewindow.screenproperty. - The
screenobject has properties that allow you to get information about the screen size and resolution. - The
screenobject has methods that allow you to interact with the screen, such as locking and unlocking the screen orientation.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on