Session Storage
In one line
Session storage is a key-value store scoped to the current tab and cleared when the tab or browser is closed.
In simple words
Session storage is one of the Web Storage APIs, alongside localStorage. It stores key-value pairs as strings, but the data is scoped to the page's tab and is wiped when the tab closes. Open the same URL in two tabs and each gets its own independent session storage.
This makes session storage ideal for transient state that should not survive a reload of the browser: a multi-step form's in-progress data, a temporary UI state, or a session token that should expire when the user closes the tab. It survives page refreshes within the same tab, unlike in-memory variables.
The API is identical to localStorage: sessionStorage.setItem(key, value), getItem, removeItem, and clear. The limit is typically 5MB per origin. For larger or structured data, use IndexedDB instead.
Example
// Save in-progress form data that survives refresh but not tab close
sessionStorage.setItem("draft-step-1", JSON.stringify({ name: "Ada", email: "ada@x.com" }));
// On page load, restore the draft
const draft = sessionStorage.getItem("draft-step-1");
if (draft) {
const data = JSON.parse(draft);
console.log("Restored:", data.name); // "Ada"
}setItem stores a JSON string. The data persists across refreshes in the same tab but is cleared when the tab closes.
Related terms
Related Resources
Related Lessons
- Client Storage
Compare session and local storage.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.