ForgeFrontend — Prepare, Practice, Crack
Secure checkout
Lifetime access
Instant Drive delivery
Free updates forever

Prepare · Practice · Crack

localStorage vs sessionStorage vs cookies?

Quick answer

localStorage persists until cleared; sessionStorage lasts for the tab session; cookies are small and sent with every HTTP request.

In detail

localStorage and sessionStorage hold ~5-10MB of strings, accessible only via JS. localStorage survives restarts; sessionStorage clears when the tab closes. Cookies are ~4KB, auto-sent to the server on each request (good for auth tokens), and support expiry and HttpOnly/Secure flags.

localStorage.setItem("theme", "dark");
sessionStorage.setItem("step", "2");
document.cookie = "token=abc; Secure; SameSite=Strict";

Why interviewers ask this: Tests practical browser-storage knowledge.

This is 1 of 118+ questions in the JavaScript Interview Kit

Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.

⚡ Get the JavaScript Interview Kit → ₹299

Full kit

JavaScript Interview Kit · ₹299

Get it →