Cookies & Local Storage Cheat Sheet
Compares cookies, localStorage, and sessionStorage APIs, attributes, size limits, and security considerations for client-side data.
2 PagesBeginnerFeb 25, 2026
localStorage & sessionStorage API
The Web Storage API and the cross-tab storage event.
javascript
// localStorage: persists until explicitly cleared, ~5-10MB per originlocalStorage.setItem('user', JSON.stringify({ id: 1, name: 'Ada' }));const user = JSON.parse(localStorage.getItem('user'));localStorage.removeItem('user');localStorage.clear(); // wipes everything for this origin// sessionStorage: same API, but scoped to a single tab and cleared on tab closesessionStorage.setItem('draft', 'unsaved text');// React to changes from OTHER tabs (storage event doesn't fire in the same tab)window.addEventListener('storage', (event) => { console.log(event.key, event.oldValue, event.newValue);});
Cookies vs localStorage vs sessionStorage
Choosing the right client-side storage mechanism.
- Cookies- ~4KB limit, sent with every HTTP request automatically, can be httpOnly/Secure
- localStorage- ~5-10MB, never sent to the server automatically, persists indefinitely
- sessionStorage- Same size/API as localStorage but cleared when the tab closes
- JS accessibility- Cookies can be hidden from JS (httpOnly); Web Storage is always JS-accessible
- Use case- Cookies for auth/session tokens sent to the server; Web Storage for client-only state
Storage Quota & Persistence
Checking how much storage is left and requesting protection from eviction.
javascript
// Estimate usage across localStorage, IndexedDB, caches, etc. (shared quota per origin)const { usage, quota } = await navigator.storage.estimate();console.log(`Using ${(usage / quota * 100).toFixed(1)}% of available storage`);// Under storage pressure, browsers can evict "best-effort" origin data.// Request persistent storage to opt out of that eviction (may prompt the user):const granted = await navigator.storage.persist();console.log('Persisted:', granted);const alreadyPersisted = await navigator.storage.persisted();
BroadcastChannel for Cross-Tab Messaging
A more direct alternative to the storage event for same-origin tab-to-tab communication.
javascript
// Unlike the 'storage' event, this doesn't require writing to localStorage// at all, and it fires in EVERY tab including ones opened later while it's active.const channel = new BroadcastChannel('app-sync');channel.postMessage({ type: 'logout' });channel.onmessage = (event) => { if (event.data.type === 'logout') { window.location.href = '/login'; }};// Close when the tab/component unmounts to free resourceschannel.close();
Pro Tip
Avoid storing sensitive tokens (session IDs, JWTs) in localStorage if you can — it has no httpOnly equivalent, so any successful XSS attack can read it directly with a single localStorage.getItem() call.
Was this cheat sheet helpful?
Explore Topics
#CookiesLocalStorage#CookiesLocalStorageCheatSheet#WebDevelopment#Beginner#SettingReadingCookies#LocalStorageSessionStorageAPI#CookieAttributes#CookiesVsLocalStorageVsSessionStorage#Security#APIs#CheatSheet#SkillVeris