localStorage stores persistent key-value pairs in the browser across sessions. Wrap values with JSON.stringify and JSON.parse for objects. Use it for user preferences like theme, font size, and sidebar state.
localStorage Helpers
// Save to localStorage
function saveData(key, value) {
localStorage.setItem(key, JSON.stringify(value));
}
// Load from localStorage
function loadData(key) {
const raw = localStorage.getItem(key);
return raw ? JSON.parse(raw) : null;
}
// Load theme on page load
$(() => {
const theme = loadData('theme') || 'light';
$('body').addClass(theme);
});