getItem() Method – How to Get a Value from Web Storage
The getItem() method retrieves the value of a web storage item.
Syntax of the getItem()
Method
getItem()
accepts one required argument. Here is the syntax:
webStorageObject.getItem(key);
webStorageObject
represents the storage object whose item you wish to get—that is,localStorage
orsessionStorage
.key
is the only argument accepted bygetItem()
. It is a required string argument specifying the name of the web storage property whose value you want to get.
Examples
Below are examples of the getItem()
method.
How to get data from session storage
- Invoke
sessionStorage
’sgetItem()
method. - Provide the name of the data you wish to get.
// Store color: "Pink" in the session storage:sessionStorage.setItem("color", "Pink");
// Get color's value from the session storage:sessionStorage.getItem("color");
// The invocation above will return: "Pink"
How to get data from local storage
- Invoke
localStorage
’sgetItem()
method. - Provide the name of the data you wish to get.
// Store color: "Pink" inside the local storage:localStorage.setItem("color", "Pink");
// Get color's value from the local storage:localStorage.getItem("color");
// The invocation above will return: "Pink"