Window sessionStorage
Example
Set and retrieve a sessionStorage name/value pair:
sessionStorage.setItem("lastname", "Smith");
sessionStorage.getItem("lastname");
Try it Yourself »
More examples below.
Description
The sessionStorage
object let you store key/value pairs in the browser.
Note
The sessionStorage
object stores data for only one session.
(The data is deleted when the browser is closed).
See Also:
The localStorage Object which stores data with no expiration date.
(The data is not deleted when the browser is closed, and are available in future sessions).
Syntax
window.sessionStorage
Or just:
sessionStorage
Save Data to Session Storage
sessionStorage.setItem("key", "value");
Read Data from Session Storage
let lastname = sessionStorage.getItem("key");
Remove Data from Session Storage
sessionStorage.removeItem("key");
Remove All (Clear session Storage)
sessionStorage.clear();
Parameters
Parameter | Description |
key | Required. The name of a key. |
value | Required. The value of the key. |
Return Value
Type | Description |
Object | A sessionStorage object. |
More Examples
Count the number of times a user has clicked a button:
if (sessionStorage.clickcount) {
sessionStorage.clickcount = Number(sessionStorage.clickcount)
+ 1;
} else {
sessionStorage.clickcount = 1;
}
Try it Yourself »
Browser Support
window.sessionStorage
is supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 8-11 | Yes | Yes | Yes | Yes |