HTML DOM Storage removeItem() method


The HTML DOM Storage removeItem() method is used for removing a storage object item by passing a given key name.

Syntax

Following is the syntax for Storage removeItem() method −

localStorage.removeItem(keyname);

OR

sessionStorage.removeItem(keyname);

Here, keyname is of type string and represents the name of the item to be removed.

Example

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

<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">Storage removeItem() method example</h1>
<p>Delete the localstorage item by clicking the below button</p>
<button onclick="itemDelete>REMOVE</button>
<p>Display the localstorage item by clicking the below button</p>
<button onclick="itemShow>DISPLAY</button>
<p id="Sample"></p>
<script>
   localStorage.setItem("TEXT1","HELLO WORLD");
   function itemDelete() {
      localStorage.removeItem("TEXT1");
      itemShow();
   }
   function itemShow() {
      var x = localStorage.getItem("TEXT1");
      document.getElementById("Sample").innerHTML ="The 'TEXT1' key value is "+x;
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the DISPLAY button −

On clicking the REMOVE button −

Updated on: 20-Aug-2019

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements