Storage Objects


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 save the data of the entire website for a long time. The saved data has no expiration time until it is manually removed.

  • sessionStorage is used to temporarily save the data of the same window (or tab), which will be deleted after closing the window or tab.

Store object properties

PropertyDescription
lengthReturns how many pieces of data are contained in the storage object.

Store object methods

PropertyDescription
key(n)Returns the name of the nth key in the stored object.
getItem(keyname)Returns the value of the specified key.
setItem(keynamevalue)Add key and value, if the corresponding value exists, update the value corresponding to the key.
removeItem(keyname)Remove key.
clear()Clear all keys in the storage object.

Web Storage API

AttributeDescription
window.localStorageStore key/value pairs in the browser. There is no expiration time.
window.sessionStorageStore key/value pairs in the browser. This data will be deleted after closing the window or tab.