Browser Storage
Persisting data in the browser — localStorage, sessionStorage, IndexedDB, cookies, and Cache API.
What it is, in plain English: Browser storage lets JavaScript save data that survives page refreshes and tab closures. localStorage stores key-value string pairs indefinitely (until cleared). sessionStorage stores them only for the current tab session. IndexedDB is a full database for large structured data. Cookies are sent to the server with every request and can be set from either client or server. Cache API stores network responses for offline use.
Saving data in the browser
By default, JavaScript variables disappear when the page refreshes. Browser storage APIs let you save data that persists — across refreshes, across sessions, and even offline.
localStorage — the most common choice
// Store a string:
localStorage.setItem("city", "Mumbai");
// Retrieve it:
console.log(localStorage.getItem("city")); // "Mumbai"
// Missing key returns null (not undefined):
console.log(localStorage.getItem("missing")); // null
// Remove one item:
localStorage.removeItem("city");
// Clear everything:
localStorage.clear();
// ─ KEY LIMITATION: everything is a string ─
// Objects must be JSON-encoded:
const user = { name: "Alice", score: 95 };
localStorage.setItem("user", JSON.stringify(user)); // store
const loaded = JSON.parse(localStorage.getItem("user")); // retrieve
console.log(loaded.name); // "Alice"
localStorage stores strings only.
Numbers, booleans, and objects must be converted with
JSON.stringify when storing and JSON.parse when reading.
Forgetting this is the most common localStorage mistake.
sessionStorage — same API, tab-only
// Same API as localStorage, but data is cleared when the tab closes:
sessionStorage.setItem("step", "2"); // wizard step
console.log(sessionStorage.getItem("step")); // "2"
// Use sessionStorage for:
// - Multi-step form data (gone if they close the tab — intentional)
// - Temporary UI state that shouldn't persist
// - Per-tab settingsA safe storage wrapper
// Wrap localStorage to handle JSON automatically and errors gracefully:
const store = {
get(key, fallback = null) {
try {
const item = localStorage.getItem(key);
return item !== null ? JSON.parse(item) : fallback;
} catch {
return fallback;
}
},
set(key, value) {
try {
localStorage.setItem(key, JSON.stringify(value));
return true;
} catch (e) {
// QuotaExceededError — storage is full
console.warn("Storage full:", e.message);
return false;
}
},
remove(key) { localStorage.removeItem(key); },
clear() { localStorage.clear(); },
};
// Usage:
store.set("settings", { theme: "dark", fontSize: 16 });
const { theme } = store.get("settings", { theme: "light" });
console.log(theme); // "dark"Cookies vs localStorage
localStorage
Cookies
Sent to server
Never
Every request
Expiry
Until cleared
Configurable
Size
~5MB
~4KB per cookie
Access
JS only
JS + server (unless HttpOnly)
Best for
Client-side preferences, cache
Sessions, auth tokens
Try It Yourself
Official Sources
- MDN Web Docs — Web Storage API
- MDN Web Docs — IndexedDB API
- MDN Web Docs — Cache
- MDN Web Docs — Using cookies
- idb — IndexedDB wrapper library
The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.