We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

The Storage interface of the Web Storage API provides access to the session storage or local storage for a particular domain, allowing you to for example add, modify or delete stored data items.

If you want to manipulate the session storage for a domain, you call Window.sessionStorage method; If you want to manipulate the local storage for a domain, you call Window.localStorage.


Storage.length Read only
Returns an integer representing the number of data items stored in the Storage object.


When passed a number n, this method will return the name of the nth key in the storage.
When passed a key name, will return that key's value.
When passed a key name and value, will add that key to the storage, or update that key's value if it already exists.
When passed a key name, will remove that key from the storage.
When invoked, will empty all keys out of the storage.


Here we access a Storage object by calling localStorage. We first test whether the local storage contains data items using !localStorage.getItem('bgcolor'). If it does, we run a function called setStyles() that grabs the data items using Storage.getItem() and uses those values to update page styles. If it doesn't, we run another function, populateStorage(), which uses Storage.setItem() to set the item values, then runs setStyles().

if(!localStorage.getItem('bgcolor')) {

function populateStorage() {
  localStorage.setItem('bgcolor', document.getElementById('bgcolor').value);
  localStorage.setItem('font', document.getElementById('font').value);
  localStorage.setItem('image', document.getElementById('image').value);

function setStyles() {
  var currentColor = localStorage.getItem('bgcolor');
  var currentFont = localStorage.getItem('font');
  var currentImage = localStorage.getItem('image');

  document.getElementById('bgcolor').value = currentColor;
  document.getElementById('font').value = currentFont;
  document.getElementById('image').value = currentImage;

  htmlElem.style.backgroundColor = '#' + currentColor;
  pElem.style.fontFamily = currentFont;
  imgElem.setAttribute('src', currentImage);

Note: To see this running as a complete working example, see our Web Storage Demo.


Specification Status Comment
HTML Living Standard
The definition of 'Storage' in that specification.
Living Standard  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support4123.5810.54
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support1818136113.2 ?
clear1818136113.2 ?
getItem1818136113.2 ?
key1818136113.2 ?
length1818136113.2 ?
removeItem1818136113.2 ?
setItem1818136113.2 ?

See also

Document Tags and Contributors

Last updated by: anton-mladenov,