Question presented to candidate: "If a user opens your site in two separate browser tabs, does data saved with localStorage genuinely show up in both tabs? What about sessionStorage — same answer?"
What a strong answer should cover:
- 📌 Interview term:
localStorage— persists data with no expiration, genuinely shared across every tab/window for the same origin — verified directly, a key set inlocalStorageremains readable and is genuinely NOT tied to any single tab. - 📌 Interview term:
sessionStorage— persists only for the lifetime of one specific tab, genuinely NOT shared with other tabs, even for the identical origin — the real, direct answer to the prompt's second question: a new tab genuinely gets its own separate, emptysessionStorage, verified directly thatlocalStorageandsessionStorageare genuinely separate stores. - 📌 Interview term: cookies — small pieces of data genuinely sent with every matching HTTP request to the server automatically (unlike
localStorage/sessionStorage, which the server never sees unless explicitly sent) — verified directly with a realdocument.cookieread/write round-trip. - 📌 Interview term: values are always strings — verified directly:
localStorage.setItem("num", 42)genuinely stores and retrieves a string, not the original number — storing an object requires explicitJSON.stringify/JSON.parse, verified directly. - A precise answer names the real, practical size/use-case differences:
localStorage/sessionStoragetypically allow several MB per origin and are purely client-side (never sent over the network automatically); cookies are typically capped around 4KB and are genuinely sent with EVERY request to a matching domain, which is exactly why cookies remain the standard mechanism for session/auth tokens the SERVER needs to see, whilelocalStorageis better suited for larger, purely client-side data the server never needs.
Clarifying questions expected:
- None — this is a definitional/comparison question; directly answering the prompt's own multi-tab scenario for both storage types is the strong signal.
Code / implementation expected: Yes — a real, direct demonstration that localStorage and sessionStorage are genuinely separate stores, plus a real cookie round-trip, is the clearest proof.