EN
JavaScript - remove item from localStorage
3 points
In this article, we would like to show you how to remove item from localStorage in JavaScript.
Quick solution:
xxxxxxxxxx
1
localStorage.removeItem('itemKey');
To remove items from localStorage
, we use removeItem()
method that takes the item key as an argument and removes that key from the storage if it exists. If there is no item with the given key, the method will do nothing.
xxxxxxxxxx
1
localStorage.removeItem('itemKey');
Note:
localStorage
is BOM (Browser Object Model) global object so we can also access it usingwindow.localStorage
.
As an alternative, you can use delete
operator to remove item from the localStorage
, however this solution is not recommended.
xxxxxxxxxx
1
delete localStorage['itemKey'];
2
3
// or:
4
5
delete localStorage.itemKey;
We use clear()
method to remove all items from the localStorage
.
xxxxxxxxxx
1
localStorage.clear();