const preferredMode = window.matchMedia("(prefers-color-scheme: dark)").matches ? "DARK" : "LIGHT"; localStorage.setItem("theme", localStorage.getItem("theme") || preferredMode); // sets theme before rendering & jquery loaded to prevent flashing of uninitialized theme // (ugly white background) document.documentElement.setAttribute("data-theme", preferredMode);