Skip to solution
easyFrontend

What is the difference between localStorage, sessionStorage, and cookies?

351 views
01

Understand the problem

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 in localStorage remains 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, empty sessionStorage, verified directly that localStorage and sessionStorage are 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 real document.cookie read/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 explicit JSON.stringify/JSON.parse, verified directly.
  • A precise answer names the real, practical size/use-case differences: localStorage/sessionStorage typically 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, while localStorage is 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.

storage
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript/browser-storage interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every storage claim below was actually run with jsdom's real storage implementations.

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: localStorage and sessionStorage are genuinely separate stores, values are always strings, and a real cookie round-trip
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 30 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track