Skip to the page

Storage and fetching · core

Cookies, localStorage, sessionStorage, IndexedDB and Cache

Cookies are small and sent with every request (use them for sessions, with HttpOnly, Secure, SameSite); localStorage persists per origin, sessionStorage per tab; both are synchronous strings; IndexedDB is async and large; the Cache API stores responses for service workers.

The notes

What you must know

  • Cookie attributes: HttpOnly (no JS access), Secure (HTTPS only), SameSite=Lax/Strict/None, Max-Age/Expires, Domain, Path. About 4 KB each.
  • localStorage: about 5 MB per origin, synchronous (blocks the main thread), strings only, readable by any script on the page (XSS can steal it).
  • sessionStorage: per tab, cleared when the tab closes; a duplicated tab gets a copy.
  • IndexedDB: async, structured data, large quotas; use a wrapper (idb). Cache API: Request/Response pairs, used by service workers.
  • The storage event fires in other tabs of the same origin when localStorage changes.

The red pen

Common traps: mistake → fix

  • Mistake: "store the JWT in localStorage, it's fine"

    Fix: any XSS can read it; an HttpOnly cookie can't be read by JS.

  • Mistake: "sessionStorage is shared between tabs"

    Fix: it is per tab.

Easy to hard

Questions interviewers ask, easy to hard

  1. Compare cookies, localStorage, sessionStorage and IndexedDB.
  2. Where do you store an auth token and why?
  3. How do tabs sync state?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview