Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Browser and Web APIs

The sessionStorage object

Reference for sessionStorage: per-tab key-value storage, methods, how it differs from localStorage, duplicated tabs, errors and typical uses.

sessionStorage has the same interface as localStorage, but its data lives only as long as the tab (more precisely, the top-level browsing context) it was written in. It survives reloads and back and forward navigation inside that tab, and it disappears when the tab or window is closed.

Each origin gets its own separate area in each tab, and two tabs on the same site do not see each other's data. That makes it a good home for short-lived state: a half-finished multi-step form, a scroll position, a filter choice or a "banner was dismissed for this visit" flag.

JavaScript
sessionStorage.setItem("step", "2");
console.log(sessionStorage.getItem("step"));

Try it

In the sandboxed preview, touching the real object throws, so guard it. Then see the behavior with a stand-in that has the same methods.

JS Real sessionStorage, guarded

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS A wizard that remembers its step

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
sessionStorage.setItem(key, value)
sessionStorage.getItem(key)
sessionStorage.removeItem(key)
sessionStorage.clear()
sessionStorage.key(index)
sessionStorage.length
Member Description
setItem(key, value) Stores the value converted to a string.
getItem(key) Returns the string, or null if missing.
removeItem(key) Deletes one key.
clear() Deletes everything for this origin in this tab.
key(index) Name at that position, or null.
length Number of stored pairs.

Exceptions: setItem() can throw QuotaExceededError when the space (typically about 5 MB) is full, and access itself can throw SecurityError when storage is blocked.

Usage notes

  • Scope is tab plus origin. Opening the same URL in a new tab starts with an empty store. Links opened with window.open() or target="_blank" may copy the opener's data at that moment, after which the two copies are independent; do not rely on it.
  • Reloads keep it. Closing the tab ends the session. Browsers that restore closed tabs may restore the data too.
  • Strings only, exactly like localStorage. Use JSON for objects.
  • No cross-tab events. The storage event on window does not carry changes between separate tabs for sessionStorage; use BroadcastChannel for tab-to-tab messages.
  • Same-origin iframes that belong to the same tab share the area with the embedding page.
  • Not secure. Any script on the page can read it, and users can edit it in developer tools. Keep secrets out.
  • Pick by lifetime. Use sessionStorage for state that should reset with the tab, localStorage for lasting preferences.

Browser support

Supported in every browser. As with localStorage, access can fail under strict privacy settings or in sandboxed frames, so wrap it in try...catch.