localStorage vs sessionStorage in JavaScript
Both localStorage and sessionStorage are
part of the Web Storage API, allowing developers to store key-value
pairs directly in a user’s web browser. While they share identical
syntax and storage limits, they differ fundamentally in their data
lifespan and scope. Understanding these differences is essential for
choosing the right storage mechanism for caching data, managing user
sessions, and maintaining application state.
Lifespan and Persistence
The primary difference between the two storage mechanisms is how long the stored data persists:
localStorage: Data persists indefinitely. It remains available even after the browser window or tab is closed, and persists across system reboots. The data is only removed if cleared explicitly via JavaScript or by the user clearing their browser cache.sessionStorage: Data exists only for the duration of the page session. As soon as the user closes the specific browser tab or window, all stored data insessionStorageis permanently cleared. Reloading or restoring the page preserves the session data, but opening a new tab creates a completely new session.
Scope and Tab Isolation
localStorage: Scoped to the document’s origin (protocol, domain, and port). Any tab or window open to the same origin can read, write, and overwrite the samelocalStoragedata simultaneously.sessionStorage: Scoped to both the document’s origin and the specific browser tab. Even if two tabs load the exact same URL, they operate with completely separatesessionStorageinstances and cannot access or modify each other’s data.
Shared Characteristics
Despite their behavioral differences, both APIs share the following technical properties:
- Capacity: Both typically offer approximately 5MB of storage per origin, which is significantly larger than the 4KB limit of HTTP cookies.
- Client-Only: Unlike cookies, data stored in
localStorageandsessionStorageis never automatically transmitted to the server with HTTP requests. - Synchronous API: Both use synchronous, blocking operations, meaning heavy read/write tasks can impact UI performance.
- Data Format: Both store data exclusively as UTF-16
strings. Objects and arrays must be serialized using
JSON.stringify()before saving and parsed withJSON.parse()when retrieved.
API Methods and Syntax
Both storage types use the exact same methods:
// Setting an item
localStorage.setItem('theme', 'dark');
sessionStorage.setItem('step', '2');
// Getting an item
const theme = localStorage.getItem('theme');
const step = sessionStorage.getItem('step');
// Removing a specific item
localStorage.removeItem('theme');
sessionStorage.removeItem('step');
// Clearing all stored data
localStorage.clear();
sessionStorage.clear();When to Use Which
- Use
localStoragefor data that should persist across multiple visits, such as persistent UI settings (theme preference, language), cached static assets, or long-term user preferences. - Use
sessionStoragefor sensitive or temporary data meant for a single transaction, such as multi-step form progress, one-time checkout flows, or tab-specific state that should reset upon closing the tab.