How to get the width of device screen in JavaScript ? Last Updated : 26 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an HTML document that is running on a device and the task is to find the width of the working screen device using JavaScript. Example 1: This example uses window.innerWidth to get the width of the device screen. The innerWidth property is used to return the width of the device. HTML <!DOCTYPE html> <html lang="en"> <head> <title> How to get the width of device screen in JavaScript? </title> </head> <body style="text-align: center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Click on Button to Get the Width of Device's Screen </h3> <button onclick="GFG_Fun()"> Click Here </button> <p id="GFG"></p> <!-- Script to display the device screen width --> <script> let elm = document.getElementById("GFG"); function GFG_Fun() { let width = window.innerWidth; elm.innerHTML = width + " pixels"; } </script> </body> </html> Output:Example 2: This example uses document.documentElement.clientWidth method to get the width of device screen. html <!DOCTYPE html> <html lang="en"> <head> <title> How to get the width of device screen in JavaScript? </title> </head> <body style="text-align: center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> Click on Button to Get the Width of Device's Screen </h3> <button onclick="GFG_Fun()"> Click Here </button> <p id="GFG"> </p> <!-- Script to display the device screen width --> <script> let elm = document.getElementById("GFG"); function GFG_Fun() { elm.innerHTML = document. documentElement.clientWidth + " pixels"; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to get the diagonal length of the device screen using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to get the height of device screen in JavaScript ? Given an HTML document which is running on a device. The task is to find the height of the working screen device using JavaScript. Prerequisite - How to get the width of device screen in JavaScript ? Example 1: This example uses window.innerHeight property to get the height of the device screen. The 2 min read How to get the Width of Scroll bar using JavaScript? Given an HTML document, the task is to get the width of the scrollbar using JavaScript. Approach:Create an element (div) containing a scrollbar.OffsetWidth defines the width of an element + scrollbar width.ClientWidth defines the width of an element.So scrollbar can be defined as width = offsetWidth 3 min read How to get the diagonal length of the device screen using JavaScript ? Knowing the width and height of a browser window helps a web developer to enhancing the user experience. It can help in improving browser animations and the relative positioning of divisions and containers. Javascript provides a window object that represent an open browser window. It provides the va 3 min read How to get the Element's Actual Width and Height in JavaScript? In JavaScript, it is often necessary to retrieve the actual width and height of an HTML element. This information can be useful in a variety of scenarios, such as setting the size of an element dynamically, centering an element on the page, or determining the size of an element for animation purpose 5 min read How to detect touch screen device using JavaScript? Sometimes you might be looking for some features to include into your web-app that should only be available to devices with a touch screen. You may need this detection while introducing newer smarter controls for touch screen users in the game app or a GPS and navigation application. While there are 3 min read Calculate the width of the text in JavaScript Calculating the width of text in JavaScript refers to determining the pixel width that a specific string of text will occupy when rendered in a browser. This is commonly done using the Canvas API or by creating a hidden element with similar styling to measure the text.Here we have some common approa 3 min read Like