How to get the current screen size/browser window with JavaScript

We can use window.innerWidth and window.innerHeight to get the current screen size of a page.

Window.innerWidth

The read-only Window property innerWidth returns the interior width of the window in pixels. This includes the width of the vertical scroll bar, if one is present. More precisely, innerWidth returns the width of the window’s layout viewport.

Example:
var w = window.innerWidth;

Window.innerHeight

The read-only innerHeight property of the Window interface returns the interior height of the window in pixels, including the height of the horizontal scroll bar, if present. The value of innerHeight is taken from the height of the window’s layout viewport.

Example:
var h = window.innerHeight;

Leave a comment

Your email address will not be published. Required fields are marked *