js get window width in javascript

To get the width of the browser window using JavaScript, you can use the window.innerWidth property. This property returns the width of the browser window including the width of the scrollbar (if present).

Alternatively, if you want to get the width of the viewport only (excluding the scrollbar), you can use document.documentElement.clientWidth.

Here's an example:

index.tsx
// get the width of the browser window
const windowWidth = window.innerWidth;

// get the width of the viewport
const viewportWidth = document.documentElement.clientWidth;
172 chars
6 lines

Note that these properties return the width of the window/viewport in pixels.

gistlibby LogSnag