window.matchMedia('(prefers-color-scheme: dark)') .addEventListener('change', event => { if (event.matches) { //dark mode } else { //light mode } })