Published Aug 2, 2023
Contribute to Docs

A Storage object allows the addition, modification, and deletion of stored key/value pairs. The keys and values stored are always UTF-16 strings. Integer keys are converted to strings. The session storage for a domain is called from an object named sessionStorage. Local storage is stored in an object called localStorage.

The two Storage objects are as follows:

  1. localStorage: Data is saved for the document’s origin, and persists across browser sessions. The data has no expiration date.
  2. sessionStorage: Data is saved for the document’s origin, but persists only for the duration of the browser session.

Note: The Storage object is particular to the protocol of the document. The localStorage or sessionStorage object for will be a different object than the localStorage or sessionStorage object returned for

Note: The behavior of Storage objects for documents loaded from the local filesystem (i.e. file:) is undefined. Many browsers define a separate Storage object for each file: URL, but it is not best practice to rely on this behavior.


The following example shows adding a key/value pair to localStorage, retrieving it, and removing it.

localStorage.setItem('dataKey', 'dataValue');
const data = localStorage.getItem('dataKey');

The example results in the following output:


Below is a list of the Storage object’s methods and properties:


Removes all keys in a Storage object.
Returns the value of the key on the given data item.
Returns the number of items in an array or the number of characters in a string.
Removes a key-value pair from the local storage based on the specified key.
Updates the value to the specified storage object key, if it exists, else adds the key to the object.

All contributors

Looking to contribute?

Learn JavaScript on Codecademy