Modern web applications often need to store small amounts of data directly in the browser. This might include user preferences, shopping cart items, form progress, or temporary application data.
The Web Storage API provides two commonly used options for this purpose: Local Storage and Session Storage.
Although both work similarly, the main difference lies in how long the stored data remains available and where it can be accessed.
What Is Web Storage?
Web Storage is a browser API that allows websites to store key-value data directly in the user’s browser.
It provides two storage mechanisms:
- localStorage
- sessionStorage
Both store data as strings and provide similar methods for adding, retrieving, and removing data.
setItem()
getItem()
removeItem()
clear()
The key difference between them is the lifetime and scope of the stored data.
What Is Local Storage?
Local Storage allows a website to store data in the browser that remains available even after the browser is closed and reopened.
The data stays stored until it is explicitly removed by the application or cleared by the user.
Example
localStorage.setItem("theme", "dark");
const theme = localStorage.getItem("theme");
console.log(theme);
Even after refreshing the page or reopening the browser, the value remains available.
Common Use Cases
Local Storage can be useful for storing:
- Theme preferences
- Shopping cart data
- Language preferences
- Non-sensitive application settings
- Data that should persist between browser sessions
What Is Session Storage?
Session Storage also stores data in the browser, but only for the duration of the current page session.
The stored data remains available while the browser tab is open and survives page reloads. However, it is cleared when that tab or window is closed.
Example
sessionStorage.setItem("currentStep", "2");
const currentStep = sessionStorage.getItem("currentStep");
console.log(currentStep);
Refreshing the page will keep the data, but closing the tab will remove it.
Common Use Cases
Session Storage can be useful for:
- Multi-step form progress
- Temporary application state
- Session-specific preferences
- Data needed only during the current tab session
Local Storage vs Session Storage
| Feature | Local Storage | Session Storage |
|---|---|---|
| Data Lifetime | Persists until manually removed | Removed when the tab or window is closed |
| Page Refresh | Data remains available | Data remains available |
| Browser Restart | Data remains available | Data is removed |
| Scope | Available to same-origin pages | Limited to the current tab session |
| API | localStorage | sessionStorage |
| Best For | Persistent browser data | Temporary session data |
Working with Objects and Arrays
One important thing to understand is that both Local Storage and Session Storage store data as strings.
For example, storing an object directly will not work as expected.
const user = {
name: "Rahul",
role: "Developer"
};
localStorage.setItem("user", user);
To properly store objects or arrays, use JSON.stringify().
localStorage.setItem("user", JSON.stringify(user));
When retrieving the data, use JSON.parse().
const storedUser = JSON.parse(
localStorage.getItem("user")
);
console.log(storedUser);
The same approach applies when working with Session Storage.
When Should You Use Local Storage?
Use Local Storage when the data needs to remain available across page reloads and future browser sessions.
For example, an application may store a user’s preferred theme:
localStorage.setItem("theme", "dark");
When the user returns to the website later, the application can retrieve the preference and apply the selected theme again.
Local Storage is best suited for data that should persist until it is intentionally removed.
When Should You Use Session Storage?
Use Session Storage when the data is only required temporarily during the current browser tab session.
For example, a multi-step form may store the user’s current progress:
sessionStorage.setItem("formStep", "3");
The user can refresh the page without losing their progress, but the data will be removed when the tab is closed.
Important Security Considerations
Local Storage and Session Storage should not be used to store sensitive information.
Avoid storing data such as:
- Passwords
- Credit card information
- Private personal information
- Sensitive authentication data
Data stored using Web Storage can be accessed by JavaScript running on the same origin. If a website is vulnerable to attacks such as Cross-Site Scripting (XSS), malicious scripts may be able to access this data.
Web Storage should therefore be used only for appropriate client-side data and not treated as a secure storage mechanism.
Conclusion
Local Storage and Session Storage provide simple ways to store data directly in the browser.
The main difference is straightforward:
Local Storage keeps data across browser sessions, while Session Storage keeps data only for the current tab session.
Use Local Storage when data needs to persist, such as theme preferences or shopping cart information.
Use Session Storage when data is temporary and only needed during the current browsing session.
Understanding when to use each storage mechanism helps you manage browser data more effectively and choose the right approach based on the needs of your application.