midHTML5
Explain the difference between LocalStorage and SessionStorage.
Updated Apr 28, 2026
Short answer
LocalStorage is persistent; SessionStorage is cleared when the tab is closed.
Deep explanation
Both are part of the Web Storage API and store data as key-value pairs (strings). LocalStorage has no expiration time. SessionStorage is unique to the specific tab/window session and survives page reloads but not tab closures.
Real-world example
Using LocalStorage to save a user's UI theme preference (Dark/Light). Using SessionStorage to store temporary state of a complex multi-page form.
Common mistakes
- Storing sensitive info (passwords/keys) in storage where it's vulnerable to XSS.
Follow-up questions
- What is the storage limit?
- Are storage values shared across subdomains?