Skip to content
Latest: JSX in React explained clearly without any framework

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 or sessionStorage.
  • key is the only argument accepted by getItem(). 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

  1. Invoke sessionStorage’s getItem() method.
  2. 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"

Try Editing It

How to get data from local storage

  1. Invoke localStorage’s getItem() method.
  2. 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"

Try Editing It