Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to get the width of device screen in JavaScript ?
Next article icon

How to get the diagonal length of the device screen using JavaScript ?

Last Updated : 27 Apr, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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 various properties which define the dimensions of your browser window. They are as follows:

  • innerWidth: It returns the width of the window’s content area.
  • innerHeight: It returns the height of the window’s content area.
  • outerWidth: It returns the width of the browser window.
  • outerHeight: It returns the height of the browser window.

Note: These dimensions are in pixels.

Since pixels are a dimensionally square unit. You cannot tell accurately how many pixels lie on the diagonal. You can approximate the diagonal by using the browser height and width and applying the Pythagoras theorem.

Pythagoras Theorem: For a right-angled triangle, hypotenuse squared is the sum of base squared and height squared.

Analogous to the above formula, Diagonal = Squared_root(Width^2 + Height^2).

Code Snippet:




<script>
function myFunction() {
  var w = window.outerWidth;
  var h = window.outerHeight;
  var d = Math.sqrt(w*w + h*h);
  console.log('Width: ' + w);
  console.log('Height: ' + h);
  console.log('Diagonal: ' + Math.ceil(d));
}
</script>
 
 

The above code will print the browser height and width in the console window. The console window can be opened using the developer tools in your browser.
Note: To get the complete screen dimensions, switch your browser to fullscreen mode. Most browsers switch the fullscreen mode using the F11 key.

The below code displays the results on the browser window using the innerHTML property of Javascript.




<!DOCTYPE html>
<html>
  
<body>
  
    <p>
        Switch to Fullscreen Mode using the F11 key.
        <br>Click the button to display the dimensions 
        of this browser window.<br> All dimensions 
        are in pixel units.
    </p>
  
    <button onclick="myFunction()">Try it</button>
  
    <p id="demo"></p>
  
    <script>
        function myFunction() {
            var i_w = window.innerWidth;
            var i_h = window.innerHeight;
            var o_w = window.outerWidth;
            var o_h = window.outerHeight;
            var d = Math.sqrt(o_w * o_w + o_h * o_h);
  
            document.getElementById("demo").innerHTML
                = "Inner Width: " + i_w +
                "<br>Inner Height " + i_h + "<br>Outer Width: "
                + o_w + "<br>Outer Height: " +
                o_h + "<br>Diagonal: " + Math.ceil(d);
        }
    </script>
</body>
  
</html>
 
 

Output:

  • Before pressing ‘Try It’ button:
  • After pressing ‘Try It’ button:

Note: If you know the linear pixel density of your monitor screen, you can divide the dimensions obtained from the above code with the density to get the dimensions in centimeters or inches.



Next Article
How to get the width of device screen in JavaScript ?
author
chitrankmishra
Improve
Article Tags :
  • HTML
  • JavaScript
  • Web Technologies
  • HTML-Misc
  • JavaScript-Misc

Similar Reads

  • How to get the width of device screen in JavaScript ?
    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. [GFGTABS] HTML
    2 min read
  • 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 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
  • 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 height of scroll bar using JavaScript ?
    To get the height of scroll bar we could use different approaches. In this article, we are given an HTML document and the task is to get the height of the scrollbar using JavaScript. Following are the different approaches to solving this problem which are discussed below: Table of Content Using Cont
    3 min read
  • How to get Camera Resolution using JavaScript ?
    In this article, we will learn to find the maximum resolution supported by the camera. We need to request camera access from the user and once access is given we can check the resolution of the video stream and find out the resolution given by the camera. The .getUserMedia() method asks the user for
    2 min read
  • How to get the div height using JavaScript ?
    In this article, we will see How to get the div height using Javascript. We can do this by following ways: Using the offsetHeight property.Using the clientHeight property.Using getBoundingClientRect() Method. Method 1: Using the offsetHeight property: The offsetHeight property of an element is a rea
    3 min read
  • How to get the position of scrollbar using JavaScript ?
    JavaScript is an amazing language and there are many functions available through which we can access any element of the HTML page through javascript. There are some simple techniques to get the scrollbar position that are discussed below: Approach 1: Whenever the function getScroll() is encountered,
    3 min read
  • How to Get and Set Scroll Position of an Element using JavaScript ?
    In this article, we will learn how to get and set the scroll position of an HTML element using JavaScript. Approach: We will be using the HTML DOM querySelector() and addEventListener() methods and the HTML DOM innerHTML, scrollTop and scrollLeft properties. We create an HTML div element with an id
    3 min read
  • How to find the height of a text in HTML canvas using JavaScript ?
    In this article, we will find the height of the text canvas using JavaScript. We have two approaches to find the height of a text in HTML canvas using JavaScript which are described below: Approach 1: In the following example, the height attribute of the HTML canvas is used. First set the font in pt
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences