Storage key() Method

Example

Get the name of the first local storage item:

var x = localStorage.key(0);

Try It Yourself

Definition and Usage

The key() method returns the name of the key with the specified index.

The key() method belongs to the Storage object and can be localStorage object, which can also be sessionStorage object.

Browser Support

Method Chrome IE Firefox Safari Opera
key() 4 8 3.5 4 10.5

Syntax

localStorage.key(index)

Or:

sessionStorage.key(index)

Parameter Value

Parameter Description
index Required. A number representing the index of the key you want to retrieve.

Technical Details

DOM Version: Web Storage API
Return value: A string value representing the name of the specified key.

More examples

Example

The same example, but using session storage instead of local storage.

Get the name of the first stored item:

var x = sessionStorage.key(0);

Try It Yourself

Example

Loop through each local storage item and display the name:

for (i = 0; i < localStorage.length; i++) {
  x = localStorage.key(i);
  document.getElementById("demo").innerHTML += x;
}

Try It Yourself

Related Pages

StorageEvent Reference Manual:key Property