Learn how to store, load, update, and remove data using localStorage and sessionStorage. Includes JSON parse/stringify patterns and safe helpers.
LocalStorage and SessionStorage in JavaScript: Save, Load, and Remove Data
The Web Storage API gives you two simple key/value stores: localStorage (persistent) and sessionStorage (cleared when the tab closes). This guide shows how to save, load, update, and remove data, including JSON handling, safe parsing, and common pitfalls. Examples are TypeScript and include the expected results.
Quick start
Result:
Storage checklist
- Store strings only: Web Storage stores strings, not objects.
- Use JSON stringify/parse for objects.
- Handle missing keys:
getItemcan returnnull. - Keep keys consistent: namespace them (e.g.,
app:theme). - Avoid secrets: localStorage is readable by any script on the page.
localStorage vs sessionStorage
localStorage persists across browser restarts.
sessionStorage is scoped to the current tab and cleared on close.
Interactive demo
Save a note in each store, then reload the page or open a new tab to compare persistence:
Result:
Save and load strings
Result:
Save and load objects with JSON
Result:
Safe JSON parsing
Storage content can be corrupted or edited by the user. Always handle parse errors.
Result:
Remove keys
Result:
Clear all storage (use carefully)
Result:
Update an existing object
Load, update, and save back. This keeps the storage consistent.
Result:
Namespacing keys
Avoid collisions by using a prefix like app:.
Result:
Storing numbers and booleans
Convert types explicitly when reading back.
Result:
Add a simple TTL (expiration)
Web Storage has no built-in expiration. You can store a timestamp and check it when reading.
Result:
Cross-tab synchronization (storage event)
The storage event fires in other tabs when a value changes -- not in the tab that made the change. Use it to keep
UI in sync across tabs.
Result (in another open tab that registered the listener):
To react to storage changes within the same tab (document), dispatch a custom event after setItem
BroadcastChanneldoes not help here either, since a channel never receives its own messages; it is useful for synchronizing with other same-origin tabs, windows, frames, or workers.
Common pitfalls
- Assuming storage is private: any script on the page can read it.
- Forgetting to stringify: objects become
"[object Object]". - Ignoring nulls:
getItemreturnsnullwhen missing. - Storing too much: localStorage limits are small (often ~5MB).
- Mixing environments: storage APIs are browser-only.
Best practices
- Use consistent key prefixes (
app:) for clarity. - Parse safely with a helper like
safeParse. - Avoid sensitive data in localStorage/sessionStorage.
- Keep payloads small for performance.
- Document your storage keys in one place.
FAQ: localStorage and sessionStorage
How do I store objects in localStorage?
Use JSON.stringify on save and JSON.parse on load.
Result:
What is the difference between localStorage and sessionStorage?
localStorage persists across browser restarts. sessionStorage is cleared when the tab closes.
Result:
How do I remove a key from storage?
Use removeItem, or clear to remove everything.
Result:
Summary
Use localStorage and sessionStorage for small, non-sensitive data. Store strings, stringify objects, parse safely, and handle missing values. For larger data or sensitive information, use a proper backend or secure storage instead.