Skip to content
tezvyn:

What is the core difference between localStorage and sessionStorage?

Source: developer.mozilla.orgEasyHow cards are made

What is the core difference between localStorage and sessionStorage?
Summary

Tab isolation versus origin-wide persistence.

Key points

sessionStorage dies with its tab; localStorage survives restarts and shares across tabs. Example: checkout form versus theme preference.

Watch out for

Claiming persistence time differs.

What's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

Which choice best explains why sessionStorage is preferred over localStorage for a multi-step checkout draft that should not leak across tabs?

  • a.sessionStorage clears when the browser is restarted, while localStorage does not
  • b.sessionStorage encrypts data at rest, unlike localStorage
  • c.sessionStorage is isolated to a single tab, preventing cross-tab data leakageCorrect
  • d.sessionStorage can store more data than localStorage
Why?

sessionStorage adds a tab-level partition, so data in one tab is invisible to another tab on the same origin, whereas localStorage is shared across all tabs. Option A is tempting but misidentifies the boundary: sessionStorage is destroyed when the tab closes, not simply when the browser restarts.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles