Browsers provide multiple storage options to store data locally on the client machine.
These storage systems help web apps remember user settings, sessions, cached data, tokens, etc.
The main storage systems are:
- LocalStorage
- SessionStorage
- Cookies
- IndexedDB
- Cache Storage (Service Workers)
- Web SQL (Deprecated)
Let's explain each one briefly and practically.
✅ 1. LocalStorage
Persistent key–value storage that remains even after browser is closed.
✔ Features:
- Storage limit ≈ 5–10 MB
- Data never expires (until we delete it)
- Only stores strings
- Synchronous API (blocking)
- Accessible via JavaScript
✔ Use Cases:
- Theme preference (dark/light mode)
- Remembering login state (NOT recommended for JWT)
- Saving user form data
- Storing app settings
Example:
localStorage.setItem("theme", "dark");
localStorage.getItem("theme");
✅ 2. SessionStorage
Similar to LocalStorage, but data exists only for one browser tab.
✔ Features:
- Storage limit ≈ 5 MB
- Clears when tab is closed
- Per-tab storage (not shared)
✔ Use Cases:
- Multi-step forms
- Temporary filters or search values
- Data that should not persist across tabs
Example:
sessionStorage.setItem("token", "12345");
✅ 3. Cookies
Small pieces of data sent with every HTTP request.
✔ Features:
- Size ≈ 4 KB
- Can have expiry time
- Used for authentication
- Can be HttpOnly (more secure)
- Can be Secure, SameSite, etc.
✔ Use Cases:
- Authentication tokens (secure HttpOnly cookies)
- Remembering logged-in sessions
- Tracking user actions (analytics)
Example:
document.cookie = "username=John; max-age=3600";
✅ 4. IndexedDB
A NoSQL database inside the browser.
Stores large and complex data, supports objects, files, blobs.
✔ Features:
- Asynchronous
- Very large storage: hundreds of MBs
- Can store structured data
- Works offline
- Supports transactions
✔ Use Cases:
- Offline apps (e.g., Google Docs)
- Storing images, files, cached API data
- Large datasets for PWAs
✅ 5. Cache Storage (via Service Worker)
Used mainly for offline-first apps (PWA).
✔ Features:
- Caches network responses
- Works even when user is offline
- Controlled by service workers
✔ Use Cases:
- Caching HTML, CSS, JS files
- Offline websites
- Faster loading (App Shell model)
Example:
caches.open("v1").then(cache => {
cache.add("/home");
});
🛑 6. Web SQL (Deprecated)
Old SQL database in the browser → not used anymore.
⚡ Comparison Table (Quick Revision)
| Feature | LocalStorage | SessionStorage | Cookies | IndexedDB | Cache Storage |
|---|---|---|---|---|---|
| Size | 5–10 MB | 5 MB | 4 KB | 100+ MB | Large |
| Expiry | Never | On tab close | Optional | Never | Never |
| Accessible by JS | Yes | Yes | Yes/No | Yes | Yes |
| Sent with every request | No | No | Yes | No | No |
| Security | Medium | Medium | High (HttpOnly) | High | High |
| Use Case | Preferences | Temporary data | Auth | Big data | Offline cache |
🎯 Final Interview Answer (Short & Clear)
The browser storage system includes LocalStorage, SessionStorage, Cookies, IndexedDB, and Cache Storage.
LocalStorage persists data permanently; SessionStorage lasts only for a tab; Cookies are small and used mainly for authentication; IndexedDB stores large structured data; and Cache Storage stores network responses for offline support.
Together, these systems help manage user sessions, preferences, caching, and offline functionality.