Example 1 — Save and Read a String
sessionStorage.setItem('username', 'JohnDoe');
var name = sessionStorage.getItem('username');
console.log(name); // JohnDoe How It Works
setItem overwrites an existing key with the same name.

Session Storage is part of the Web Storage API. It lets you save simple key-value data in the browser for as long as the current tab stays open. Unlike localStorage, which persists across browser restarts, sessionStorage is cleared when the user closes the tab.
Use it for temporary state: wizard steps, form drafts, filters on a listing page, or anything you do not need to share with other tabs. This tutorial covers the API, comparisons with other storage options, and plain HTML Try It demos.
Save strings.
Read values.
Delete one key.
Wipe tab data.
Not shared.
When to pick.
Session Storage is a browser mechanism for storing key-value pairs as strings. Each key maps to one value for the lifetime of the page session in that tab.
A page session:
sessionStorage.Data is scoped to origin (protocol + domain + port) and tab. Two tabs on example.com do not see each other’s sessionStorage values.
Browsers expose a global sessionStorage object in JavaScript. It behaves like a simple dictionary:
For large structured data or offline databases, use IndexedDB instead. For tiny strings that must persist forever across visits, use localStorage.
The sessionStorage object provides four essential methods:
sessionStorage.setItem(key, value) — stores a string under key.sessionStorage.getItem(key) — returns the string or null if missing.sessionStorage.removeItem(key) — deletes one entry.sessionStorage.clear() — removes every key in this tab.You can also use bracket syntax: sessionStorage['theme'] = 'dark' (still stores strings only).
Helpful properties:
sessionStorage.length — number of keys.sessionStorage.key(index) — name of the key at position index.// Storing data
sessionStorage.setItem('username', 'JohnDoe');
// Retrieving data
var username = sessionStorage.getItem('username');
console.log(username); // JohnDoe
// Missing key returns null
console.log(sessionStorage.getItem('missing')); // null var user = { name: 'Jane', score: 42 };
sessionStorage.setItem('user', JSON.stringify(user));
var saved = JSON.parse(sessionStorage.getItem('user'));
console.log(saved.name); // Jane Delete one key or wipe the entire session store for the tab:
// Remove one key
sessionStorage.removeItem('username');
// Remove everything in this tab's sessionStorage
sessionStorage.clear(); Call removeItem when a user logs out of a single-page flow. Call clear sparingly—it deletes all keys your app stored in that tab.
| Feature | sessionStorage | localStorage |
|---|---|---|
| Lifetime | Until tab closes | Until cleared manually |
| Shared across tabs | No (per tab) | Yes (same origin) |
| Survives reload | Yes | Yes |
| Typical use | Wizard state, tab drafts | Theme, login token, prefs |
QuotaExceededError.localStorage plus the storage event, or BroadcastChannel, to sync across tabs."42"). Objects need JSON.stringify.| Task | Code |
|---|---|
| Save | sessionStorage.setItem('key', 'value') |
| Read | sessionStorage.getItem('key') |
| Delete one | sessionStorage.removeItem('key') |
| Delete all | sessionStorage.clear() |
| Count keys | sessionStorage.length |
| Store object | setItem('k', JSON.stringify(obj)) |
setItem()Write
getItem()Read
one tabIsolated
stringValues
Five short examples from basic save/read to a full HTML page. Try It Yourself demos use plain HTML with no CSS so you can focus on the API.
sessionStorage.setItem('username', 'JohnDoe');
var name = sessionStorage.getItem('username');
console.log(name); // JohnDoe setItem overwrites an existing key with the same name.
var cart = { items: 2, total: 19.99 };
sessionStorage.setItem('cart', JSON.stringify(cart));
var loaded = JSON.parse(sessionStorage.getItem('cart'));
console.log(loaded.total); // 19.99 Always wrap JSON.parse in try/catch if data might be corrupted.
sessionStorage.setItem('temp', 'draft');
sessionStorage.removeItem('temp');
console.log(sessionStorage.getItem('temp')); // null removeItem on a missing key does not throw an error.
for (var i = 0; i < sessionStorage.length; i++) {
var key = sessionStorage.key(i);
console.log(key, sessionStorage.getItem(key));
} Useful for debugging what your app stored in the current tab.
Save and load a user name — from the reference tutorial, improved with auto-load on open:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Session Storage Example</title>
</head>
<body>
<h1>Session Storage Example</h1>
<input type="text" id="input" placeholder="Enter your name">
<button id="save">Save</button>
<button id="load">Load</button>
<p id="result"></p>
<script>
document.getElementById('save').onclick = function () {
sessionStorage.setItem('userName', document.getElementById('input').value);
};
document.getElementById('load').onclick = function () {
var userName = sessionStorage.getItem('userName');
document.getElementById('result').textContent =
userName ? 'Hello, ' + userName : 'No name saved';
};
</script>
</body>
</html> Reload the Try It page after saving—the name remains until you close the tab.
Browser creates empty sessionStorage for this tab.
Your script saves a string.
Data stays in the same tab.
Page reads saved state on load.
All sessionStorage for that tab is cleared.
localStorage and the storage event.sessionStorage is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge. It is part of the HTML5 Web Storage specification.
sessionStorage is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge. It is part of the HTML5 Web Storage specification.
Bottom line: Safe to use in all current projects. Feature-detect with typeof sessionStorage !== 'undefined' in very old environments.
checkoutStep, draftNote)Session Storage is a useful tool for storing data temporarily during a web session. It provides a straightforward way to manage tab-scoped state with setItem, getItem, removeItem, and clear.
Choose sessionStorage when data should disappear with the tab. Choose localStorage for long-lived preferences, or IndexedDB for structured offline data. Try the demos below to see the API in action.
Use these points when picking client-side storage.
Isolated.
ScopeNot forever.
DurationJSON helps.
TypeData stays.
PersistNo secrets.
SafetyOpening a link with target="_blank" creates a new tab with fresh sessionStorage, even on the same site. Duplicate tabs do not copy session data—only localStorage is shared across tabs.
Save a name, reload the page, and see it persist until you close the tab—plain HTML, no CSS required.
5 people found this page helpful