JavaScript Storage Object
The Web Storage API provides two storage objects, sessionStorage (session storage) and localStorage (local storage), to add, delete, modify, and query data on web pages.
-
localStorage is used to persistently save data for the entire website; the saved data has no expiration time until manually removed.
-
sessionStorage is used to temporarily save data for the same window (or tab); the data is deleted after the window or tab is closed.
Storage Object Properties
| property | Description |
|---|---|
| length | Returns the number of data items contained in the storage object. |
Storage Object Methods
| Method | Description |
|---|---|
| key(n) | Returns the n name of the nth key in the storage object. |
| getItem(keyname) | Returns the value of the specified key. |
| setItem(keyname, value) | Adds a key and value; if the corresponding value already exists, updates the value for that key. |
| removeItem(keyname) | Removes a key. |
| clear() | Clears all keys in the storage object. |
Web Storage API
| property | Description |
|---|---|
| window.localStorage | Stores key/value pairs in the browser. No expiration time. |
| window.sessionStorage | Stores key/value pairs in the browser. The data is deleted after the window or tab is closed. |