Skip to the page

DOM & browser APIs · Trade-off

Storage options: cookies vs localStorage vs sessionStorage vs IndexedDB

The interviewer asks

Compare cookies, localStorage, sessionStorage and IndexedDB. Where would you store a cart, a JWT, user preferences, and 50 MB of offline data?

Then

Follow-ups the interviewer asks next

  1. Size limits and sync vs async (localStorage blocks the main thread).
  2. Cookie attributes: HttpOnly, Secure, SameSite, Domain, Path, Partitioned.
  3. The storage event for cross-tab sync.
  4. Safari ITP eviction of script-written storage, and storage quotas or navigator.storage.persist().

The answer

What a strong answer covers

  • Cookies (about 4 KB, sent with every request, can be HttpOnly); localStorage (about 5 MB per origin, strings, persistent, sync); sessionStorage (per tab); IndexedDB (large, async, structured, available in workers)
  • None of the script-accessible stores are safe from XSS
  • Cache API for request/response pairs (service workers)

The red pen

Common traps

Avoid:"localStorage is secure because it's per-domain".

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

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

Start your free interview