r/javascript • • Aug 21 '18

Effectively supported in all browsers, JavaScript has its own form of media queries just like CSS.

https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
465 Upvotes

114 comments sorted by

View all comments

-2

u/[deleted] Aug 22 '18 edited Aug 22 '18

Can anyone answer why this would be better than using window.innerWidth?

Because I've been using window.innerWidth to check if I should render a mobile version of React component and it did its job without problem (that I know of).

Edit: I don't know what the problem is of these people downvoting me? I'm asking a normal question. Your downvotes are just rude.

4

u/Treolioe Aug 22 '18

They have little in common. You might be able to solve your problem with innerWidth. But some problems can be based on that users can resize the window or change the viewport orientation. This is generally handled with media queries in css, but you might want to handle this dynamically in JS as well.