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
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
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance