What is the core difference between localStorage and sessionStorage?

Tab isolation versus origin-wide persistence.
sessionStorage dies with its tab; localStorage survives restarts and shares across tabs. Example: checkout form versus theme preference.
Claiming persistence time differs.
WHAT THIS TESTS: This question checks whether you understand the Web Storage API partitioning model beyond simple persistence. Many developers know localStorage sticks around, but the interviewer wants to hear that sessionStorage is scoped to a single browser tab and its embedded iframes of the same origin, whereas localStorage is scoped to the origin across all tabs and survives browser restarts. It also tests whether you can translate that distinction into a practical architectural choice.
A GOOD ANSWER COVERS: A strong answer hits four things in order. First, persistence: sessionStorage is ephemeral and destroyed when the tab closes, while localStorage persists until explicitly cleared. Second, scope: sessionStorage is isolated per tab even for the same origin, so two tabs on the same site cannot read each others sessionStorage; localStorage is shared across every tab and window for that origin. Third, the mechanism: both are synchronous key-value stores partitioned by origin, but sessionStorage adds an extra tab-level partition. Fourth, a concrete decision framework: choose sessionStorage when you want state to live only for the current navigation session without leaking across tabs, and choose localStorage when you want user preferences or cached data to remain available globally and across restarts.
COMMON WRONG ANSWERS: The biggest red flag is saying the only difference is how long data lasts. Another weak answer is claiming sessionStorage survives a page refresh but not a tab close without clarifying the tab-scope boundary. Some candidates incorrectly state that localStorage is shared across different origins or that sessionStorage is shared across tabs. Bringing up cookies unprompted as a comparison is fine only if you keep it brief; rambling about cookies instead of answering the scope question directly signals a lack of focus.
LIKELY FOLLOW-UPS: An interviewer might ask how much data you can store; the MDN article points to separate documentation on storage quotas and eviction criteria rather than giving a single limit. They may ask about the synchronous nature of both APIs and why heavy storage operations can block the main thread, or they might pivot to IndexedDB as an asynchronous alternative. Another common follow-up is third-party iframe access: if a user disables third-party cookies, embedded iframes lose access to both storage types. You might also be asked to compare Web Storage with cookies, which the article notes are a less intuitive way to store key-value pairs.
ONE CONCRETE EXAMPLE: Imagine an e-commerce checkout flow with three steps. If you store the partially completed form in localStorage, opening the same site in a second tab would show the unfinished draft, which could confuse the user or merge two unrelated purchase attempts. Instead, you should store the draft in sessionStorage. This keeps the draft tied to the specific tab and clears it automatically when the tab closes, preventing cross-tab pollution while still surviving accidental refreshes. Conversely, store the users dark-mode preference in localStorage so that every new tab and future session respects their choice without re-querying the server.
Source: developer.mozilla.org
Read the original → developer.mozilla.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.