CodeZettaInterview Hub

JavaScript · Browser APIs · Junior · Comparison

Compare localStorage, sessionStorage, and IndexedDB for client-side persistence.

Short Interview Answer

localStorage persists string key-values; sessionStorage lasts per tab session; IndexedDB stores larger structured data asynchronously.

Detailed Explanation

Web Storage is synchronous, string-only, ~5MB-ish (varies), and blocks the main thread on large reads/writes. sessionStorage is isolated per tab. IndexedDB is async, transactional, supports indexes and larger quotas — better for offline apps. Alternatives: Cache API for Request/Response, cookies for small server-sent state (with security caveats). Never store secrets in Web Storage on XSS-prone apps. Prefer structured cloning in IDB over `JSON.stringify` trees when appropriate.

Interview Tip

Mention sync vs async and XSS risk.

Common Mistake

Putting large JSON blobs in localStorage and wondering why the UI janks.

Did you know this answer?