How to detect dark mode using JavaScript

Search for a command to run...

No comments yet. Be the first to comment.
JavaScript's Numeric Separators provide an enhanced way to improve the readability of large numeric literals by allowing developers to use underscores as visual separators. This feature makes it easier to understand and maintain code that involves la...

If you are operating a website, spam can be a big problem for you. Bots and spammers can exploit your website by sending unwanted messages, registering fake user accounts, or injecting malicious content. Luckily, there are many ways to protect your w...

In today's world, almost every website or application requires a username to be unique for every user. A username is an identifier for a user to login or access a website or application. However, generating a unique username from a string can be chal...
Regular expressions (regex) are powerful tools in JavaScript that allow you to match and manipulate text based on specific patterns. In this blog post, we will explore how to use regex in JavaScript to match strings that contain only alphabets or alp...
Nowadays, most operating systems and applications have started implementing dark mode. It's a feature that switches the application theme from light to dark, providing users with better readability, improved visual ergonomics, and reduced eye-strain. Web applications are no exception. Many websites now have a dark mode option, which can be triggered automatically based on the user's system preference. However, you can also detect whether or not the user has enabled dark mode, using JavaScript.
In this tutorial, we will see how to detect if the user is in dark mode, in their browser, using JavaScript. We will make use of the window.matchMedia() method and CSS media queries to achieve this.
In CSS, Media Queries are used to apply styles based on certain conditions such as screen resolution, device orientation, or color scheme.
For example, one can set a different font size for a specific screen width by using media queries.
Consider the following example:
@media only screen and (max-width: 768px) {
/* Styles for devices with a maximum width of 768px */
body {
font-size: 16px;
}
}
In this example, the CSS media query applies the defined styles for devices whose screen width is less than or equal to 768px.
Let's get back to our main topic; detecting dark mode using JavaScript. When a user enables dark mode on their device, the browser sets the prefers-color-scheme media query of that device to dark.
Therefore, we can check if this media query is true or false to detect whether dark mode is enabled or not.
Here is a basic implementation to detect whether the user has dark mode enabled on their device or not.
const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
if (isDarkMode) {
// User is in dark mode
console.log("User is in dark mode");
} else {
// User is not in dark mode
console.log("User is not in dark mode");
}
We first check whether the window.matchMedia() method is supported by the user's browser. If the browser does not support the matchMedia() method, it returns undefined, and the if statement evaluates to false, bypassing the check.
We then call the matchMedia() method with the argument (prefers-color-scheme: dark). This creates a MediaQueryList object that matches devices that have enabled dark mode.
We then check the matches property of the MediaQueryList. If the matches property is true, the user is in dark mode, and we perform the necessary actions. If it's false, the user is in light mode.
That's it! We have seen how to detect if a user has dark mode enabled on their device or not, using JavaScript. We can use this feature to switch between the light and dark themes of a website or perform various actions based on the user's color scheme preference. I hope this tutorial helps you in creating better user experiences for your website visitors.