Day 2: localStorage & sessionStorage
Daily Objective
Section titled “Daily Objective”Persist user data across browser refreshes with localStorage.
1. Web Storage API
Section titled “1. Web Storage API”// Saving dataconst preferences = { theme: 'dark', fontSize: 18 };localStorage.setItem('userPrefs', JSON.stringify(preferences));
// Retrieving dataconst stored = localStorage.getItem('userPrefs');const parsedPrefs = stored ? JSON.parse(stored) : null;
// Clearing// localStorage.removeItem('userPrefs');// localStorage.clear();Daily Hands-on Practice (70%)
Section titled “Daily Hands-on Practice (70%)”- Store a user’s dark mode preference in
localStorageand restore it when the page loads.