localStorage in JavaScript, sessionStorage & Cookies

Medium100% Free~25 mins#localstorage#sessionstorage#cookies#httponly#samesite#web-storage#browser-apis#security
Interview Answer (30-Second Summary)

localStorage in JavaScript, like sessionStorage (together the Web Storage API), stores client-side key-value string pairs without sending data to servers, while HTTP Cookies store small snippets that are transmitted with every HTTP request. Cookies support critical security flags like HttpOnly, Secure, and SameSite to mitigate XSS and CSRF.

Key Learning Objectives
✓

Compare the 4 primary browser persistence mechanisms: localStorage, sessionStorage, Cookies, and IndexedDB.

✓

Understand Web Storage string coercion mechanics and why objects serialize to '[object Object]' without JSON.stringify.

✓

Master cookie security attributes: HttpOnly (XSS defense), Secure (HTTPS only), and SameSite (CSRF defense).

✓

Understand the cross-tab storage event and why it only triggers in other windows of the same origin.

✓

Learn storage quotas, synchronous blocking trade-offs, and why sensitive tokens should never be stored in localStorage.

The Interview Problem

What is logged to the console when the following Web Storage operations execute, and how do implicit string coercion and serialization dictate stored types?

1class StorageMock {
2 constructor() {
3 this.store = new Map();
4 }
5 setItem(k, v) {
6 this.store.set(String(k), String(v));
7 }
8 getItem(k) {
9 const val = this.store.get(String(k));
10 return val !== undefined ? val : null;
11 }
12}
13
14const storage = new StorageMock();
15
16// 1. Storing object without serialization
17storage.setItem('user', { id: 1 });
18const rawUser = storage.getItem('user');
19
20// 2. Storing number and boolean
21storage.setItem('count', 42);
22storage.setItem('active', false);
23const countType = typeof storage.getItem('count');
24const activeVal = storage.getItem('active');
25
26// 3. Proper JSON serialization
27storage.setItem('session', JSON.stringify({ token: 'xyz' }));
28const parsedToken = JSON.parse(storage.getItem('session')).token;
29
30console.log(rawUser, countType, activeVal, parsedToken);
Predict Console Output
Interactive Challenge

Select the option that matches what this code prints to the console:

[object Object] string false xyz

{"id":1} number false xyz

[object Object] string boolean xyz

null string false null

V8 Engine Execution Trace
Step 1 of 5 (Line 14)

StorageMock instance is created. Models the W3C Web Storage specification where keys and values are coerced to DOMString.

Call Stack (Top = Active)
Global Execution Context
Lexical Scope / Bindings
storage:StorageMock { store: Map(0) }
Console Stream
> [empty]

localStorage in JavaScript, sessionStorage & Cookies Explained

Client-Side Storage Architecture Matrix

Storage MechanismCapacityLifetimeScopeAccessible ByTransmission with HTTP Requests
localStorage~5-10 MBPersistent across browser restartsOrigin (proto+host+port)JavaScript onlyNo
sessionStorage~5 MBTab lifetime (cleared on tab close)Specific browser tabJavaScript onlyNo
Cookies~4 KBConfigurable (Expires/Max-Age)Domain & PathJS & Server (unless HttpOnly)Yes, automatically on every request
IndexedDB> 250 MBPersistentOriginJavaScript (async)No

The localStorage String Coercion Trap

Web Storage only stores strings. When a non-string value is passed to setItem(key, value), the browser engine implicitly converts it to a string:

javascript
localStorage.setItem('auth', { authenticated: true });
localStorage.getItem('auth'); // Returns '[object Object]'!

localStorage.setItem('isLoggedIn', false);
const status = localStorage.getItem('isLoggedIn'); // Returns string 'false'
if (status) {
  // BUG: In JavaScript, any non-empty string is TRUTHY!
  console.log('User is logged in!'); // Fires even though value is 'false'!
}

To safely store structured data, always use JSON.stringify() on write and JSON.parse() with a try...catch on read.

Cookie Security Attributes

Cookies are sent with every matching HTTP request, making them critical for authentication and vulnerable if misconfigured:

  1. HttpOnly: Prohibits client-side scripts from reading document.cookie. This is the primary defense against token theft via Cross-Site Scripting (XSS).
  2. Secure: Instructs the browser to only transmit the cookie over encrypted HTTPS connections.
  3. SameSite: Controls whether cookies are sent on cross-site requests to defend against Cross-Site Request Forgery (CSRF):
    • Strict: Never sent on cross-site requests (e.g. following an external link to your site).
    • Lax (modern default): Sent on top-level GET navigations, but blocked on cross-site POST/fetch requests.
    • None: Sent on all requests (requires Secure flag).

The Cross-Tab storage Event

When localStorage is mutated, the browser dispatches a storage event on window:

  • Critical Nuance: The event fires on all other tabs/windows sharing the same origin, but never in the tab that executed the setItem or removeItem call!
  • This is commonly used in production to synchronize authentication logout across all open browser tabs.
Common Traps & Mistakes

Storing JWT access tokens or sensitive user credentials in `localStorage`. Any XSS script injection can instantly read and exfiltrate all `localStorage` data.

Checking boolean flags in storage with `if (localStorage.getItem('flag'))`. The return value is the string `'false'`, which is truthy.

Expecting `sessionStorage` to be shared between two separate browser tabs navigating to the same URL. `sessionStorage` is strictly scoped to an individual tab.

Assuming the `storage` event fires in the tab that mutated `localStorage`. It only fires in other concurrent tabs of the same origin.

FAANG Follow-Up Probes
Probe #1

How does the Web Storage synchronous blocking API impact main-thread performance when reading large JSON payloads (>5MB)?

Probe #2

What is the difference between sessionStorage behavior when opening a new tab manually versus window.open()?

Probe #3

How does the Cache API differ from IndexedDB for progressive web applications (PWAs)?

localStorage in JavaScript, sessionStorage & Cookies Interview Questions

Key interview inquiries, edge cases, and architectural follow-ups for Web Storage, Cookies & Persistence.

Q1.
What are the differences between localStorage, sessionStorage, and Cookies?

localStorage persists indefinitely across browser restarts; sessionStorage persists only for the lifetime of a single browser tab; Cookies expire based on an Expires/Max-Age header and are automatically sent to the server in HTTP headers on matching requests.

localStorage and sessionStorage offer ~5MB to 10MB of storage per origin. Cookies are limited to 4KB per cookie and approximately 50 cookies per domain.

HttpOnly blocks client-side JavaScript access via document.cookie (protecting session tokens from XSS). Secure ensures cookies are only transmitted over HTTPS. SameSite (Strict/Lax/None) prevents cross-site request forgery (CSRF).

Any malicious script injected via an XSS vulnerability can read localStorage.getItem("token") and exfiltrate it. HttpOnly cookies cannot be accessed by JavaScript, providing stronger defense-in-depth.

Web Storage only supports strings. Storing an object directly (localStorage.setItem("key", obj)) coerces it to "[object Object]". You must serialize objects using JSON.stringify() and deserialize via JSON.parse().

When localStorage is updated in one tab, a "storage" event is fired on the window object in all other open tabs of the same origin, enabling real-time multi-tab state coordination.

More JavaScript interview questions for freshers