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?

Upgrade to Pro for quizzes, tracked progress and a completion certificate for just $25 🚀

Want more developer tips, tricks and tools?
Then follow me:
FREE Coding Career Quick Start Guide 🚀
Discover the best way to learn to code, how to land a job and valuable resources to help your journey in this free 15 page value packed guide.
Looking to email me? You can get me on my first name at allthecode.co