localStorage & sessionStorage
Persist data in the browser, store objects as JSON and handle errors and the storage event.
Refresh a normal web page and every variable is gone. The Web Storage API gives each website a small key–value store in the browser that survives reloads — perfect for preferences, drafts, and progress tracking. (Elephantoo itself remembers your completed lessons this way!)
Two stores, same API#
Both are scoped to the origin (protocol + domain + port): https://elephantoo.com can't read another site's storage. Browsers typically allow around 5 MB per origin.
The basic methods#
Open DevTools → Application (Chrome/Edge) or Storage (Firefox) → Local Storage to see and edit stored values.
Web Storage is a browser API; it doesn't exist in Node.js. Run these examples in the browser console.
Everything is a string#
Use JSON for anything that isn't a plain string:
A safe storage helper#
Reading storage can fail in real life: the key might be missing, the JSON might be corrupted by an old version of your app, storage can be full, and some privacy modes throw on access. Wrap it once and use the helper everywhere:
Example: remember dark mode#
The saved choice wins; otherwise we follow the operating system's preference.
Example: auto-save a draft#
Using sessionStorage means the draft survives an accidental reload but not closing the tab. Switch to localStorage if it should survive longer.
Versioning and expiry#
Data outlives your code. Add a version (and, if needed, an expiry time) so a future release can safely ignore stale data:
Namespacing keys ("elephantoo:theme", "draft:comment") also avoids collisions with libraries.
Syncing tabs with the storage event#
When one tab changes localStorage, other tabs of the same origin receive a storage event:
The tab that made the change does not get the event.
Limits and alternatives#
- Synchronous: every call blocks the main thread. Fine for small values; don't store megabytes or call it in tight loops.
- Strings only, ~5 MB: for large or structured data (offline apps, files), use IndexedDB (via a helper library such as
idb). - Not secure: any JavaScript on the page can read it. Never store passwords, credit card numbers or long-lived auth tokens. Use HttpOnly cookies for sessions — JavaScript can't read those at all.
- Cookies are sent to the server with every request; Web Storage never is.
- Users can clear storage anytime. Treat it as a cache or convenience, not a database.
Common mistakes#
- Storing objects without
JSON.stringify. - Calling
JSON.parseonnullor corrupt data without atry/catchor fallback. - Using storage in code that also runs in Node (e.g. server-side rendering) — check
typeof localStorage !== "undefined". - Assuming the
storageevent fires in the same tab.
What's next#
You've finished the browser module! Next, go deeper into the language itself, starting with one of its most misunderstood features: this, plus call, apply and bind.
Check your understanding
Quick quiz
1.What happens when you run
localStorage.setItem("prefs", { theme: "dark" })?2.How long does
sessionStoragedata last?3.Which of these should you NOT store in localStorage?
Finished reading?
Mark this lesson complete to track your progress.