FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

useWindowSize

15mins

Implement a utility useWindowSize() that returns the current height and width of the window.

It should automatically update whenever the window is resized, triggering re-renders (in React) or updates in plain JS.

Your task: Implement useWindowSize so that it:

  • Returns an object with { width, height }.
  • Updates values whenever the window dimensions change.
  • Cleans up the resize listener when no longer needed.

Example

const screen = useWindowSize(); // Suppose window.innerWidth = 1200, window.innerHeight = 800 console.log(screen); // { width: 1200, height: 800 }

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests