JavaScript Window matchMedia() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

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)
CallWhat 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.

Library declarations
JavaScript
matchMedia(mediaQueryString)

Parameters

ParameterDescription
mediaQueryStringRequired. CSS media query to parse
change eventOptional. Fires when match result flips

Return Value

Return ValueDescription
*MediaQueryList object

JavaScript Examples

Example 1 — Basic matches

Check whether the viewport is 600px wide or less.

JavaScript
const mql = window.matchMedia("(width <= 600px)");
console.log(mql.matches); // true or false
Try It Yourself

How It Works

  1. Pass a query. Features stay inside parentheses.
  2. Get a list. Return value is a MediaQueryList.
  3. Read matches. Boolean for the current document.
  4. One-shot. This does not keep updating until you listen.

Example 2 — change Listener

Update text whenever the match result flips (for example on resize).

JavaScript
const mql = window.matchMedia("(min-width: 600px)");

function onChange(e) {
  console.log(e.matches ? "wide enough" : "too narrow");
}

onChange(mql); // initial
mql.addEventListener("change", onChange);
Try It Yourself

How It Works

  1. Create the list. Same matchMedia call.
  2. Run once. Call your handler with the current list.
  3. Subscribe. Use addEventListener("change", ...).
  4. Use e.matches. The event carries the new boolean.

Example 3 — Mobile Branch

Choose compact vs full UI from a max-width query.

JavaScript
const mobile = window.matchMedia("(max-width: 767px)");

if (mobile.matches) {
  console.log("Mobile layout");
} else {
  console.log("Desktop layout");
}
Try It Yourself

How It Works

  1. Pick a breakpoint. Here: 767px and below.
  2. Branch on matches. Keep JS and CSS breakpoints aligned.
  3. Optional. Add a change listener for live switches.
  4. Tip. Prefer CSS for pure styling; use JS for behavior.

Example 4 — Dark Preference

Detect the user’s prefers-color-scheme: dark setting.

JavaScript
const dark = window.matchMedia("(prefers-color-scheme: dark)");
console.log(dark.matches ? "Dark preferred" : "Light preferred");

dark.addEventListener("change", function (e) {
  console.log(e.matches ? "Switched to dark" : "Switched to light");
});
Try It Yourself

How It Works

  1. Query preference. Not about viewport width — system setting.
  2. Read once. matches is the current preference.
  3. Listen. change fires if the user flips OS theme.
  4. Use case. Toggle a theme class or load alternate assets.

Example 5 — Reduced Motion

Skip fancy animation when the user prefers reduced motion.

JavaScript
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)");

if (reduce.matches) {
  console.log("Skip animation");
} else {
  console.log("Play intro animation");
}
Try It Yourself

How It Works

  1. Ask accessibility. Respect prefers-reduced-motion.
  2. Branch. Skip or simplify motion when matches is true.
  3. Pair with CSS. You can also gate keyframes in CSS @media.
  4. Why JS. Useful for canvas, libraries, or timed sequences.

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
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% 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).

Next: moveBy()

Move the browser window by a relative offset.

moveBy() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful