I believe that there is no equivalent solution in javascript, however, there are ways to arrive at convenient results.
The first form would be the combination of the event that detects changes in the width of the window
, together with a check of the desired width:
window.onresize = function(event) {
if (window.innerWidth < 768) {
console.log("Largura da janela menor que 768 px");
}
};
The second way would be using the method window.matchMedia
, as the example below:
if (window.matchMedia("(min-width: 500px)").matches) {
console.log("A viewport tem pelo menos 500 pixels de largura");
} else {
console.log("A viewport tem menos que 500 pixels de largura");
}
And through this question in the Soen, found that there is a plugin with the proposal to do the equivalent of CSS media queries in javascript:
https://github.com/paulirish/matchMedia.js/
The
@media
serves for various things, not only screen sizes, so the existing answer is wrong, or better just half correct.– Guilherme Nascimento