Open In App

HTML | DOM Storage key() Method

The DOM Storage key() Method is related to the Storage object and is used to return the name of the key with the specified index. Storage object can be either a localStorage or sessionStorage Object. 

Syntax:



localStorage.key(index)
sessionStorage.key(index)

Parameters: It accepts a parameter i.e. index to get the name of the key with that particular given index.

Return Values: It returns the name of the key in the form of the string.



Below is the HTML code to show the working of HTML DOM Storage key() Object:
Example:




<!DOCTYPE html>
<html>
<head>
    <title>
        HTML DOM Storage key() Method
    </title>
    <!-- Script to get the name of the key -->
    <script>
        function myGeeks() {
            var key = localStorage.key(0);
            document.getElementById(
            "geeks").innerHTML = key;
        }
    </script>
</head>
 
<body>
    <h1>GeeksForGeeks</h1>
    <h2>DOM Storage key() Method</h2>
    <br>
    <button onclick="myGeeks()">
        Submit
    </button>
    <p id="geeks"></p>
  
</body>
</html>

Output:

Supported Browsers: The browser supported by DOM Storage key() Method are listed below:

Article Tags :