HTML Academy · Lesson

localStorage vs sessionStorage

Understand the lifetime and scope of each storage type.

Lesson 1 of 413 steps

localStorage vs sessionStorage is a free HTML Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, and your progress syncs across the web and the CoddyKit app. The HTML Academy course includes 4 lessons in total.

Web Storage API Overview

The Web Storage API provides two client-side key-value stores: localStorage (persistent across sessions) and sessionStorage (cleared when the tab closes). Both store string values and are accessible synchronously via JavaScript on the same origin.

localStorage Persistence

Data in localStorage persists until explicitly cleared by code, the user, or browser data deletion. It survives browser restarts, page refreshes, and tab closes. Capacity is typically 5-10MB per origin depending on browser.

sessionStorage Scope

sessionStorage is scoped to the current browser tab and session. Data survives page refreshes within the same tab but is cleared when the tab closes. Two tabs to the same site have separate, isolated sessionStorage — they do not share data.

API Similarity

Both storages share the same API: setItem(key, value), getItem(key), removeItem(key), clear(), and length property. The same code works for both — just replace localStorage with sessionStorage and vice versa.

Choosing Between Them

Use localStorage for: user preferences (theme, language), cached API responses, remember-me functionality. Use sessionStorage for: wizard/form step state, temporary shopping cart before checkout, data that should not persist across visits for privacy reasons.

Same-Origin Restriction

Both storages are strictly origin-scoped (protocol + host + port). https://example.com cannot read localStorage set by http://example.com. This is a security boundary — subdomains are also separate origins unless document.domain is manipulated (deprecated).

Storage Capacity

Both have ~5MB per origin limit in most browsers. Exceeding the limit throws a QuotaExceededError. Large data storage needs require IndexedDB which supports gigabytes of structured data. Web Storage is for small key-value pairs, not binary blobs or large datasets.

Synchronous Nature

Web Storage reads and writes are synchronous and block the main thread. For large reads or writes, this can cause jank. Prefer IndexedDB for frequent large data operations. Keep Web Storage values small and infrequent to avoid performance issues.

Serialization Pattern

Web Storage stores only strings. Serialize objects with JSON.stringify(); deserialize with JSON.parse(). Handle parse failures: try { return JSON.parse(stored) } catch { return defaultValue }. Corrupt or non-JSON values in storage throw parse errors.

Storage in Private/Incognito Mode

localStorage in private/incognito mode is cleared when all private windows are closed — behaving like sessionStorage. Code relying on localStorage for authentication state should handle the case where localStorage is wiped unexpectedly.

Third-Party Restrictions

Browsers increasingly restrict third-party storage access. An iframe from third-party.com on first-party.com cannot access third-party.com's localStorage. Safari's ITP and Firefox's Enhanced Tracking Protection partition storage — first-party usage is unaffected.

Knowledge Check

When is sessionStorage cleared?

Summary

localStorage persists indefinitely until cleared programmatically or by the user; sessionStorage clears when the tab closes. Both are origin-scoped, synchronous string key-value stores with ~5MB capacity. Use localStorage for preferences and persistent state; use sessionStorage for temporary, session-scoped state that should not survive tab close.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “localStorage vs sessionStorage” lesson free?

Yes — any 3 lessons of the HTML Academy learning path, including “localStorage vs sessionStorage”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The HTML Academy course includes 4 lessons in total.

What will I learn in “localStorage vs sessionStorage”?

Understand the lifetime and scope of each storage type. You practise HTML Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start HTML Academy?

No prior experience is required. HTML Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “localStorage vs sessionStorage” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this HTML Academy lesson?

Yes. Every HTML Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. localStorage vs sessionStorage
  2. Getting Setting and Removing Items
  3. Listening for Storage Events
  4. Security What Never to Store in Storage
← Back to HTML Academy