Skip to content

Detect dark mode in JavaScript


Check if a user’s device is in dark mode with the following code snippet.

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

console.log(isDarkMode) // result: true or false
See also  How to represent negative infinity in JavaScript?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.