window.matchMedia() returns a MediaQueryList for a CSS media query string so you can check if the document matches (MDN). Read .matches, or listen for change when the result flips.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call window.matchMedia. Start with this line.
JavaScript
matchMedia(mediaQueryString)
Call
What it does
matchMedia(mediaQueryString)
Primary signature.
window.matchMedia(...)
Call with the arguments your case needs.
Built into the browser. Call window.matchMedia(...) (or the global matchMedia(...)) — no import required.
Branch. Skip or simplify motion when matches is true.
Pair with CSS. You can also gate keyframes in CSS @media.
Why JS. Useful for canvas, libraries, or timed sequences.
Compatibility
Browser Support
Window.matchMedia() is marked Baseline Widely available on MDN (since July 2015). Browser Window API — not a Node.js built-in.
✓ Baseline · Widely available
matchMedia()
Test CSS media queries from JavaScript across modern browsers.
99%Core tag support
ChromeYes
Full support
EdgeYes
Full support
FirefoxYes
Full support
SafariYes
Full support
OperaYes
Full support
HTML element100% supported
Bottom line: Safe for responsive JS. Prefer addEventListener('change') over legacy addListener.
Frequently Asked Questions
It parses a CSS media query string and returns a MediaQueryList. Read .matches to see if the document matches right now, and listen for change to react when that result flips.
Yes for media features. MDN: matchMedia("(width <= 600px)") works; matchMedia("width < 600px") does not. Keywords like screen and operators like and do not need extra parentheses.
Use mediaQueryList.addEventListener("change", handler). Prefer that over the older addListener() API.
addListener() and removeListener() exist mainly for backward compatibility. Modern code should use addEventListener("change") and removeEventListener("change").
innerWidth is only a pixel number. matchMedia() understands full media query syntax—orientation, prefers-color-scheme, prefers-reduced-motion, and compound conditions.
No. It only reports whether a query matches. Your JavaScript decides what to do next (toggle classes, skip animations, load modules, and so on).