How to detect dark mode in JavaScript
Thursday, December 11, 2025
Detecting the user’s dark mode preference allows you to provide a better user experience by respecting their system settings.
As the creator of CoreUI with over 25 years of JavaScript development experience, I’ve implemented dark mode detection in countless production applications.
The most reliable solution is to use the window.matchMedia() API to check the prefers-color-scheme media query.
This method works across all modern browsers and respects the user’s operating system preferences.