localStorage vs sessionStorage
Understand the lifetime and scope of each storage type.
localStorage vs sessionStorage is a free HTML Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “localStorage vs sessionStorage” lesson free?
Yes — the full text of “localStorage vs sessionStorage” is free to read here on the web, and the HTML Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the HTML Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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
- localStorage vs sessionStorage
- Getting Setting and Removing Items
- Listening for Storage Events
- Security What Never to Store in Storage