The key() method retrieves a web storage item’s name (key).
Syntax of the key()
Method
key()
accepts one required argument. Here is the syntax:
webStorageObject
represents the storage object whose key you wish to get—that is, localStorage
or sessionStorage
.
index
is the only argument accepted by key()
. It is a required integer argument specifying the index of the item whose key you wish to get.
Examples
Below are examples of the key()
method.
How to get the name of an item in session storage
- Invoke
sessionStorage
’s key()
method.
- Provide the index of the item whose name you wish to get.
Try Editing It
How to get the name of an item in local storage
- Invoke
localStorage
’s key()
method.
- Provide the index of the item whose name you wish to get.
Try Editing It