Skip to content
🧠 Master React Package Creation — Claim Your Spot

getItem() Method – How to Get a Value from Web Storage

The getItem() method retrieves the value of a web storage item.

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.

Below are examples of the getItem() method.

  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

  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