30 Days Of JavaScript

Day 19: LocalStorage

Lesson 4: Deleting data fromLocalStorage

Deleting data from LocalStorage is pretty simple. We use the handy .removeItem() function.

Let's add a button to delete all numbers. Add this to your HTML

1<button id="delete">Delete</button>

And this to your JavaScript

1const deleteButton = document.getElementById("delete");
2deleteButton.onclick = () => {
3  localStorage.removeItem("numbers");
4  numbers = [];
5
6  while (ul.hasChildNodes()) {
7    ul.removeChild(ul.children[0]);
8  }
9};

We remove the item from LocalStorage, we reset the numbers array in memory and then we remove all the numbers from the <ul> in the DOM.

If you want totally clear all the items you have saved in LocalStorage then you have this function as well:

1localStorage.clear();

Outline

Go Pro?

If you upgraded to pro, sign in here

  • About
  • Blog
  • Privacy
Looking to email me? You can get me on my first name at allthecode.co