LocalStorage.removeItem(): Remove an item by key from LocalStorage.
Similarly, remove "Ramesh" value from storage.
Syntax
window.localStorage
The syntax for SAVING data to localStorage:
localStorage.setItem("key", "value");
The syntax for READING data from localStorage:
var lastname = localStorage.getItem("key");
The syntax for REMOVING data from localStorage:
localStorage.removeItem("key");
localStorage.removeItem() Method Example
First, we add a key and value pair to localstorage.
localStorage.setItem("firstName", "Ramesh");
Where "firstName" is the key and "Ramesh" is the value. Also, note that localStorage can only store strings.
To store arrays or objects you would have to convert them to strings.
To do this we use the JSON.stringify() method before passing to setItem() .
var user = {
firstName : "Ramesh",
lastName : "Fadatare"
}
localStorage.setItem("id", JSON.stringify(user));
Now, let's use the removeItem() method to remove the item from the key. Let's remove the user object previously stored using setItem() method:
localStorage.removeItem('id');
localStorage.removeItem("firstName");
Comments
Post a Comment