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:
localStorage.removeItem('itemKey');
Practical example
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.
localStorage.removeItem('itemKey');
Note:
localStorage
is BOM (Browser Object Model) global object so we can also access it usingwindow.localStorage
.
Alternative solution
As an alternative, you can use delete
operator to remove item from the localStorage
, however this solution is not recommended.
delete localStorage['itemKey'];
// or:
delete localStorage.itemKey;
Remove all items
We use clear()
method to remove all items from the localStorage
.
localStorage.clear();