HTML DOM Storage key() method

HTMLWeb DevelopmentFront End Technology

The HTML DOM Storage key() method is used for returning the key name at a given index in a storage object. The index is passed as a parameter to key() method. The storage object can be a session object or localStorage object.


Following is the syntax for −

Storage key() method using localStorage −


Storage key() method using sessionStorage −


Here, index is of type integer representing the key number that you want to get the name for.


Let us look at the example for the Storage key() method −

<!DOCTYPE html>
<h1 style="text-align:center">storage key() method example</h1>
<p>Get the first object key name by clicking on the below button</p>
<button onclick="keyName()">GET NAME</button>
<p id="Sample"></p>
   function keyName() {
      var k = localStorage.key(1);
      document.getElementById("Sample").innerHTML ="The key name of the second
      localStorage object is "+k;


This will produce the following output −

On clicking the GET NAME button −

Published on 20-Aug-2019 09:29:38