Skip to main content

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

info

The getItem() method will return null if its argument does not exist in the specified web storage.

Overview

This article discussed what web storage's getItem() method is. We also used examples to see how it works.

Tweet this article