English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Storage getItem() Method

JavaScript Storage Object

getItem()Method returns the value of the specified key name in the given storage object.

If the specified key does not exist, this method returnsnull.

The getItem() method belongs to the storage object and can belocalStorageObject orsessionStorrageObject.

Syntax:

localStorage.getItem(keyName)
sessionStorage.getItem(keyName)
var x = localStorage.getItem("image");
Test and See‹/›

Browser Compatibility

The numbers in the table specify the first browser version that fully supports the getItem() method:

Method
getItem()43.511.549

Parameter Value

ParameterDescription
keyNameA string that contains the name of the key to retrieve its value

Technical Details

Return value:A string containing the key value specified. If the specified key does not exist, it returns null
DOM Version:Web Storage API

More examples

Returns the value of the specified session storage item:

var x = sessionStorage.getItem("age");
Test and See‹/›

You can also use dot notation to get values:

var x = localStorage.url;
Test and See‹/›

The following functions retrieve three data items from local storage and then use them to set custom styles on the page:

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;
   root.style.backgroundColor = currentColor;
   p.style.fontFamily = currentFont;
   img.setAttribute(' src' currentImage);
}
Test and See‹/›

Related References

HTML Tutorial:Web Storage API

Window (Window) Reference:window.localStorage Property

Window (Window) Reference:window.sessionStorage Property

JavaScript Storage Object