JavaScript Storage Objects
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 permanently 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). This data will be 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 nth in the storage objectnkey name |
| getItem(keyname) | Returns the value of the specified key. |
| setItem(keyname, value) | Adds keys and values. If the corresponding value exists, updates the value corresponding to 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 will be deleted after the window or tab is closed. |