Storage key() Method
Example
Get the name of the first local storage item:
var x = localStorage.key(0);
Try it Yourself »
Description
The key() method returns name of the key with the specified index.
The key() method belongs to the Storage Object, which can be either a localStorage object or a sessionStorrage object.
Browser Support
| Method | |||||
|---|---|---|---|---|---|
| key() | 4 | 8 | 3.5 | 4 | 10.5 |
Syntax
localStorage.key(index)
Or:
sessionStorage.key(index)
Parameter Values
| Parameter | Description |
|---|---|
| index | Required. A Number representing the index of the key you want to get the
name of |
Technical Details
| DOM Version: | Web Storage API |
|---|---|
| Return Value: | A String, 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 storage item:
var x = sessionStorage.key(0);
Try it Yourself »
Example
Loop through each local storage item and display the names:
for (i = 0; i < localStorage.length; i++) {
x = localStorage.key(i);
document.getElementById("demo").innerHTML += x;
}
Try it Yourself »
Related Pages
StorageEvent reference: The key Property