Skip to the solution
EasyFrontend6 min read

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

351 views

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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 30 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

How does Array.prototype.toReversed() avoid the classic bug of accidentally mutating a shared array reference?

Continue