About Lesson
Real-Life Example
Remembering a Dark Mode Setting
JavaScript
<label> Toggle Mode: </label>
<input type="checkbox" name="toggle-darkmode" id="toggle-darkmode">
// Save preference
const savePreference = (isDarkMode) => {
localStorage.setItem("darkMode", isDarkMode);
};
// Load preference
const loadPreference = () => {
const darkMode = localStorage.getItem("darkMode") === "true";
document.body.style.backgroundColor = darkMode ? "black" : "white";
};
// Simulate toggling
document.getElementById("toggle-darkmode").addEventListener("click", () => {
const isDarkMode = document.body.style.backgroundColor === "white";
savePreference(isDarkMode);
loadPreference();
});