Media queries in JS
Tags: mediaquery, css, javascript
Published: 2021-02-18 10:52:41

[Original article - written by Marko Ilic](https://css-tricks.com/working-with-javascript-media-queries/) 

Media query is a CSS technique introduced in CSS3.

CSS media queries are a core ingredient in any responsive design. They’re a great way to apply different styles to different contexts.

The old way to detect media queries in JS is using window innerWidth as sample code below:

```javascript
function checkMediaQuery() {
  // If the inner width of the window is greater then 768px
  if (window.innerWidth > 768) {
    doResponsiveThings();
  }
}

window.addEventListener('resize', checkMediaQuery);
```

## Using matchMedia() 
To determine if the document matches the media query string in JavaScript, we use the [matchMedia()](https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia) method. Even though it’s officially part of the [CSS Object Model View Module specification](https://www.w3.org/TR/cssom-view-1/%23dom-window-matchmedia) which is in Working Draft status, the browser support for it is great going as far back as Internet Explorer 10 with 98.6% global coverage.

[Caniuseit](https://caniuse.com/?search=matchmedia)

![Caniuseit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ebpa1j0fdb403f5pgmw.png)

The usage is nearly identical to CSS media queries. We pass the media query string to matchMedia() and then check the .matches property.

```javascript
const mediaQuery = window.matchMedia('(min-width: 768px)')
```

The defined media query will return a MediaQueryList object. It is an object that stores information about the media query and the key property we need.


```javascript
const mediaQuery = window.matchMedia('(min-width: 768px)')
// Check if the media query is matched
if (mediaQuery.matches) {
  doResponsiveThings();
}
```

That’s the basic usage for matching media conditions in JavaScript. But there’s more to it. For example, if we were change the window size below our target window size, nothing updates the way it will with CSS right out of the box. 

### Listen for changes
 MediaQueryList has an addListener() (and the subsequent removeListener()) method that accepts a callback function that’s invoked when the media query status changes. we can fire additional functions when the conditions change, allowing us to “respond” to the updated conditions.

```javascript
const mediaQuery = window.matchMedia('(min-width: 768px)')

function handleTabletChange(e) {
  // Check if the media query is true
  if (e.matches) {
    doResponsiveThings();
  }
}

// Register event listener
mediaQuery.addListener(handleWindowChange)

// Initial check
handleWindowChange(mediaQuery)
```

We explored how matchMedia() allows us to define media conditions and examined the MediaQueryList object. We also saw the “old” way of doing things by listening for resize events on the window. While it’s still widely used and a totally legit way to respond to changes to the size of the window.innerWidth, it’s unable to perform checks on advanced media conditions.

Looking at the performance impact comparing with the old way:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/end6v54yvgykippiwped.png)

More about [@media queries - A Complete Guide to CSS Media Queries - by Andrés Galante] (https://css-tricks.com/a-complete-guide-to-css-media-queries/?utm_source=CSS-Weekly&utm_campaign=Issue-430&utm_medium=web)


== Update from @RVxLab [comment](https://dev.to/rvxlab/comment/1bi3p)
 
MediaQueryList.addListener is marked as [deprecated](https://caniuse.com/?search=addlistener), MediaQueryList extends from EventTarget so you can use addEventListener instead. Except for Safari 13 & earlier we can do fallback condition as below:

```javascript

// Register event listener
if (mediaQuery.addEventListener) {
  mediaQuery.addEventListener('change', handleWindowChange);
} else {
  mediaQuery.addListener(handleWindowChange);
}
```