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.