![]() |
VOOZH | about |
JavaScript sessionStorage is a web storage technique that stores data for the duration of a page session. The sessionStorage object lets you store key/value pairs in the browser. It allows setting, retrieving, and managing data that persists only until the browser tab or window is closed, ensuring data accessibility within the same tab.
window.sessionStorage;| Method | Description |
|---|---|
setItem(key, value) | Sets the data in the sessionStorage with the passed key and value. |
getItem(key) | Returns the value of the key passed to it, if it is stored in the storage. |
removeItem(key) | Removes the passed key with its value from the storage. |
storage.length | Returns the total number of stored items in the storage. |
key(index) | Returns the key stored at the passed index. |
clear() | Clears all the stored items. |
In this example, sessionStorage is used to store, retrieve, and remove key-value pairs. Buttons trigger functions that either set, get, or remove items from sessionStorage, with results displayed in an HTML element.
Output:
JavaScript sessionStorage is a powerful tool for temporarily storing data within a user's session. It provides simple methods to set, retrieve, and manage key-value pairs, ensuring information is available until the browser tab closes. Using sessionStorage enhances user experience by maintaining state and preferences throughout the session.