localStorage vs sessionStorage
Choose the right storage lifetime.
localStorage vs sessionStorage is a free JavaScript Academy lesson on CoddyKit — lesson 2 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 JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Stores, Same API
localStorage and sessionStorage share identical methods. The difference is lifetime: how long the data survives.
localStorage Persists
Data in localStorage stays until explicitly removed. It survives page reloads, tab closes, and browser restarts.
localStorage.setItem("theme", "dark");
// Still there days later, after closing the browsersessionStorage Is Temporary
Data in sessionStorage lasts only for the tab session. Closing the tab clears it.
sessionStorage.setItem("step", "2");
// Gone once the tab is closedPer-Tab Isolation
Each browser tab gets its own sessionStorage. Two tabs of the same site have separate session stores; they do NOT share data.
localStorage Is Shared
By contrast, all tabs and windows of the same origin share one localStorage. A write in one tab is visible to the others.
Reload Behavior
A normal reload keeps sessionStorage for that tab. Duplicating a tab may copy the session, but opening a fresh tab starts empty.
sessionStorage.setItem("draft", text);
// Survives F5 reload in the same tabChoosing Between Them
Use localStorage for long-lived preferences (theme, language). Use sessionStorage for transient per-tab state (a wizard step, a one-time form draft).
Security Caution
Neither store is secure. Both are readable by any script on the page, so never store passwords or sensitive tokens that must stay private.
Same Methods Everywhere
Because the APIs match, you can swap one for the other by changing only the object name. Code written against one works against the other.
const store = useSession ? sessionStorage : localStorage;
store.setItem("k", "v");Private Browsing
In private/incognito modes both stores typically clear when the session ends, and some browsers limit or disable them. Always handle the possibility of a failed write.
try {
localStorage.setItem("k", "v");
} catch (e) {
console.log("storage unavailable");
}Quota Is Separate
Each store has its own quota (commonly around 5MB per origin). Filling sessionStorage does not eat into the localStorage budget.
Quick Check
Comparing the two stores.
Recap
Both stores share the same API but differ in lifetime. localStorage persists across sessions and is shared by all tabs of an origin; sessionStorage lives only for one tab session and is isolated per tab. Neither is secure, and each has its own ~5MB quota.
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 JavaScript 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 JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “localStorage vs sessionStorage”?
Choose the right storage lifetime. You practise JavaScript 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 JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 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 JavaScript Academy lesson?
Yes. Every JavaScript 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
- Storing and Reading Data
- localStorage vs sessionStorage
- Storing Objects with JSON
- Storage Events and Limits