JavaScript’s window.matchMedia (vs resize event listeners)

JavaScript’s window.matchMedia (vs resize event listeners)

JavaScript’s window.matchMedia (vs resize event listeners). Why you should use window.matchMedia when checking for window resizes in Javascript. It can be quite common when working with web apps to need to use Javascript to check for window resizes. The typical way is to use an event listener on window's resize event.

It can be quite common when working with web apps to need to use Javascript to check for window resizes.

The typical way is to use an event listener on window's resize event, such as:

function checkForWindowResize() {
    console.log(`Screen width: ${window.innerWidth}`);

    if (window.innerWidth < 600) {
       doSomethingForSmallScreens();
    }
    else {
       doSomethingForLargeScreens();
    }
}

window.addEventListener('resize', checkForWindowResize);

However, this will mean that the checkForWindowResize function will fire every time the window is resized, at every pixel increment/decrement.

This can have huge performance issues if the event listener function is slow to run.

It is quite common that you don't need all of those events - you only care if it was resized smaller or larger than a certain size.

In those cases there is a nicer alternative where you can use css media query breakpoints. This won't be suitable for all uses of adding an event listener to the resize event, but it is very useful in many situations.

javascript programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Learning JavaScript: Development Environments for JavaScript Programming

One of the nice things about learning JavaScript these days is that there is a plethora of choices for writing and running JavaScript code. In this article, I’m going to describe a few of these environments and show you the environment I’ll be using in this series of articles.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Developer Career Path: To Become a Team Lead or Stay a Developer?

For a developer, becoming a team leader can be a trap or open up opportunities for creating software. Two years ago, when I was a developer, ... by Oleg Sklyarov, Fullstack Developer at Skyeng company

JavaScript Programming Basics - Object Oriented Programming

In this JavaScript tutorial, we're going to dive into Object Oriented Programming with JavaScript. JavaScript is a multi-paradigm language, so OOP isn't the only one you must use, but that's the one I am going to cover now.

filterMap in JavaScript, efficiency for functional programming

All JavaScript developers are awesome and they deserve a brilliant way of filtering and mapping over an array with just one function with a linear complexity O(n) of one run through the array.