Cookies and Browser Storage
When to use cookies, localStorage, sessionStorage, and IndexedDB
Skills taught: web.cookies, web.localstorage, web.sessionstorage, web.storage-choice
Storage options compared
Storage Capacity Expiry Sent to server? JS access? Use case
────────────── ──────── ───────────────── ─────────────── ────────── ──────────────────────
Cookie 4 KB Set by server Yes, every req Yes* Session tokens, auth
localStorage ~5 MB Never (manual) No Yes Preferences, cache
sessionStorage ~5 MB Tab closed No Yes Temp tab state, draft
IndexedDB 100s MB Never (manual) No Yes Large data, offline
* HttpOnly cookies cannot be read by JavaScript (document.cookie)
Cookies in detail
Server sends:
Set-Cookie: sessionId=abc123;
HttpOnly; ← JS cannot read this cookie (XSS protection)
Secure; ← Only sent over HTTPS
SameSite=Strict; ← Not sent in cross-site requests (CSRF protection)
Max-Age=86400; ← Expires in 24 hours
Path=/ ← Sent for all paths on this domain
Browser sends in every subsequent request:
Cookie: sessionId=abc123
Without HttpOnly:
document.cookie → 'sessionId=abc123; theme=dark'
An XSS attack could steal the session token!
With HttpOnly:
document.cookie → 'theme=dark' (sessionId is hidden from JS)
localStorage and sessionStorage
// localStorage — persists until explicitly cleared
localStorage.setItem('theme', 'dark');
localStorage.setItem('fontSize', '16');
const theme = localStorage.getItem('theme'); // 'dark'
const missing = localStorage.getItem('xyz'); // null
localStorage.removeItem('theme'); // delete one key
localStorage.clear(); // delete everything
// Store objects (JSON serialization required)
localStorage.setItem('user', JSON.stringify({ name: 'Alice', score: 95 }));
const user = JSON.parse(localStorage.getItem('user'));
// sessionStorage — same API, but clears when tab closes
sessionStorage.setItem('currentStep', '3');
sessionStorage.getItem('currentStep'); // '3'
// → null after tab is closed and reopened
Warning: Never store sensitive data in localStorage. All JavaScript on the page — including third-party scripts and XSS injections — can read localStorage. Store session tokens in HttpOnly cookies instead.
Frequently Asked Questions
What is the SameSite cookie attribute and why was it added?
SameSite prevents CSRF (Cross-Site Request Forgery) attacks. Without SameSite, if you are logged into bank.com and visit evil.com, evil.com can make your browser send a request to bank.com and your session cookie goes along for the ride. SameSite=Strict prevents cookies from being sent in any cross-site request. SameSite=Lax (the modern browser default) prevents it in sub-resource requests but allows it in top-level navigation (clicking a link from another site). SameSite=None allows cross-site cookies but requires Secure attribute.
Is localStorage secure?
localStorage is origin-scoped (only your origin can read it) but accessible to all JavaScript running on that origin — including third-party scripts (analytics, ads, CDN libraries). Any XSS vulnerability gives an attacker full access to localStorage. Do not store sensitive information (tokens, private data) in localStorage. Use HttpOnly cookies for session tokens, and consider the sensitivity of anything you cache there.