Session Storage is a Web Storage API that allows you to store data as key-value pairs in the browser. Unlike Local Storage, the data exists only for the current browser tab or window and is automatically removed when the tab is closed.
๐ What is Session Storage?
Session Storage is accessed using the sessionStorage object. It is useful for temporarily storing data needed during a user's current browsing session.
๐ก Why Use Session Storage?
- ๐ Save form data while filling a page
- ๐ Store temporary shopping cart information
- ๐ Preserve search filters during navigation
- ๐ Store multi-step form progress
- โก Keep temporary application state
๐ Session Storage Methods
| Method | Description |
|---|---|
| setItem() | Stores a value. |
| getItem() | Retrieves a value. |
| removeItem() | Removes a specific item. |
| clear() | Removes all stored items. |
| key() | Returns the key at a given index. |
| length | Returns the number of stored items. |
๐ Storing Data
Use setItem() to save a value.
Store a Value
sessionStorage.setItem("username", "John");๐ Reading Data
Read a Value
const username = sessionStorage.getItem("username");
console.log(username);โ๏ธ Updating Data
Updating is done by storing a new value using the same key.
Update a Value
sessionStorage.setItem("username", "Alice");๐๏ธ Removing Data
Remove a Value
sessionStorage.removeItem("username");๐งน Clearing All Data
Clear Session Storage
sessionStorage.clear();๐ Checking the Number of Items
Length Property
console.log(sessionStorage.length);๐ Accessing Keys
Get Key
console.log(sessionStorage.key(0));๐ฆ Storing Objects
Session Storage stores only strings. Convert objects into JSON before saving.
Store an Object
const user = {
name: "John",
age: 25,
city: "New York"
};
sessionStorage.setItem(
"user",
JSON.stringify(user)
);๐ฅ Retrieving Objects
Retrieve an Object
const user = JSON.parse(
sessionStorage.getItem("user")
);
console.log(user.name);
console.log(user.city);๐ Storing Arrays
Store an Array
const colors = ["Red", "Green", "Blue"];
sessionStorage.setItem(
"colors",
JSON.stringify(colors)
);๐ค Reading Arrays
Retrieve an Array
const colors = JSON.parse(
sessionStorage.getItem("colors")
);
console.log(colors);๐ Helper Functions
Save Data
saveData()
function saveData(key, value) {
sessionStorage.setItem(
key,
JSON.stringify(value)
);
}Load Data
loadData()
function loadData(key) {
const data = sessionStorage.getItem(key);
return data
? JSON.parse(data)
: null;
}Delete Data
deleteData()
function deleteData(key) {
sessionStorage.removeItem(key);
}๐ Complete Example
Temporary User Session
const session = {
username: "John",
currentPage: "Dashboard",
lastVisited: Date.now()
};
// Save
sessionStorage.setItem(
"session",
JSON.stringify(session)
);
// Read
const currentSession = JSON.parse(
sessionStorage.getItem("session")
);
console.log(currentSession);
// Remove
sessionStorage.removeItem("session");๐ Session Storage vs Local Storage vs Cookies
| Feature | Session Storage | Local Storage | Cookies |
|---|---|---|---|
| Storage Limit | ~5 MB | ~5โ10 MB | ~4 KB |
| Lifetime | Until tab closes | Until manually removed | Configurable |
| Shared Between Tabs | โ No | โ Yes | โ Yes |
| Sent to Server | โ No | โ No | โ Yes |
โ ๏ธ Limitations
- ๐ฆ Only strings can be stored directly.
- ๐ซ Data is lost when the browser tab or window is closed.
- ๐ Accessible only within the same origin (protocol, domain, and port).
- ๐ Not suitable for storing sensitive information.
Note
โ Best Practices
- ๐ Use it for temporary data only.
- ๐ง Store objects and arrays using JSON.stringify().
- ๐ฅ Retrieve complex data using JSON.parse().
- ๐งน Remove unused data with removeItem() or clear().
- ๐ Never store passwords or confidential information.
๐ฏ Summary
Session Storage is ideal for storing temporary browser data that should persist only during the current tab session. It offers the same API as Local Storage but automatically clears data when the tab or window is closed, making it perfect for temporary user state, multi-step forms, and session-specific information.