How to detect dark mode in JavaScript

In this Article we will go through how to detect dark mode only using single line of code in JavaScript.

Let's define this short function:

const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;