Check media queries and detect changes with JavaScript matchMedia()
window.matchMedia() returns a MediaQueryList for a CSS media query. Read matches to check it now. Listen for change when the result flips. For styling alone, use CSS @media.
Create a media query and read matches
(min-width: 600px) tests whether the document viewport is at least 600 CSS pixels wide. The viewport is the area displaying the page. Run the snippet in the console. media shows the query text and matches is the current Boolean. See CSSOM View Module.
const query = window.matchMedia('(min-width: 600px)');
console.log(query.media); // '(min-width: 600px)'
console.log(query.matches);
Storing const wide = query.matches captures the Boolean at that moment. Read query.matches again for the current value or listen for changes to run code when it flips.
Listen for changes and remove the listener
change fires when the Boolean switches, so resizing from 601px to 650px does not trigger an event for this query. Use query.matches for the initial update and event.matches for later changes.
const query = window.matchMedia('(min-width: 600px)');
function update(matches) {
console.log(matches ? "Wide viewport" : "Narrow viewport");
}
function handleChange(event) {
update(event.matches);
}
update(query.matches);
query.addEventListener('change', handleChange);
function stopWatching() {
query.removeEventListener('change', handleChange);
}
Call stopWatching() when your component is removed or no longer needs updates. Pass the same handleChange function to removeEventListener() that you passed when registering. Cross the breakpoint in the console example, then call the function to stop the logs. See DOM Standard.
Separate CSS styling from JavaScript behavior
Change colors or spacing with @media as shown below. Use JavaScript when you need to update a message or control behavior. For example, when prefers-reduced-motion: reduce matches, a script can skip a nonessential animation.
#panel { background: #e8f4ff; }
@media (min-width: 600px) {
#panel { background: #e6f4e9; }
}
If CSS and JavaScript share a breakpoint, update both when it changes. addListener() and removeListener() remain for compatibility. Use addEventListener('change', ...) and removeEventListener() in new code.
Complete example: cross the 600px breakpoint
Save the HTML as match-media.html in UTF-8. Open it in a browser and use the developer tools to set the viewport to 599px if available. Switch to 600px, then 601px, then back to 599px without reloading.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Check viewport width with matchMedia</title>
<style>
body { margin: 24px; font: 16px/1.6 sans-serif; }
#panel { padding: 20px; background: #e8f4ff; }
@media (min-width: 600px) {
#panel { background: #e6f4e9; }
}
</style>
</head>
<body>
<h1>Compare viewport widths</h1>
<p>Resize the browser viewport across the 600px breakpoint.</p>
<div id="panel">CSS changes this box’s background at the same 600px breakpoint.</div>
<p>JavaScript result: <strong id="state" aria-live="polite"></strong></p>
<p>Breakpoint crossings: <span id="changes">0</span></p>
<script>
const query = window.matchMedia('(min-width: 600px)');
const state = document.querySelector('#state');
const changes = document.querySelector('#changes');
let changeCount = 0;
function render(matches) {
state.textContent = matches ? "Wide viewport (600px or more)" : "Narrow viewport (below 600px)";
changes.textContent = String(changeCount);
}
query.addEventListener('change', event => {
changeCount += 1;
render(event.matches);
});
render(query.matches);
</script>
</body>
</html>
At 600px, the query becomes true and the counter reaches 1. At 601px it stays at 1. Back at 599px, the query becomes false and the counter reaches 2. CSS controls the background, while JavaScript updates the message and count.




