Transcript of the tutorial.
TOP
All right, then.
So we've seen how to set items in local storage and also get items from local storage.
So in this case, we're setting a name and an age, and then we're getting those and logging them to
the console.
And if I save this, we should see those over here.
And if I also type out local storage, then we should see a length of two as well as these values.
Now how do we delete data from local storage?
What if we've done with that and we want to then delete it so it's not stored on the computer anymore?
Well, there's a couple of different methods we could use.
The first one I'm going to show you is to remove a single item.
So we'd say local storage, first of all, then use a method called remove item.
And the item we want to remove is whatever the key is.
So for example, name, and that is going to remove the name value from local storage.
So if I save this now and come over here, we still see Mario and 50 because we actually logged those
out before we remove the item.
But if I now try to say name is now equal to local storage again and then get the name property, so
get item name if I do this and then console dot log name down here, then it shouldn't work and we can
see null.
So now since we've removed the item, we get null for this value.
Okay.
We can also now go to the application panel and we can see that name has now gone.
So that's what this method does right here.
It removes an item from local storage.
Now what if we wanted to remove all items from local storage?
Well, let me just comment this out again and save it so that it recreates these.
And we should see now we have both of them again.
Now, if I want to remove all of the items from local storage, I could say local storage and then dot
clear.
And that method doesn't take any arguments.
It just clears out local storage.
So anything that we've added in the past from this web application we've removed now.
So now if I duplicate this and try to get the age as well, let me say age is equal to local storage
dot get item and age and we try to log these out, then we should still see them to begin with.
Then we clear local storage, then we try to get them again.
So they both should be null down here.
So if we go to console we can see we get them to begin with, then they are null.
And inside the application panel, local storage, there are no values anymore.
So they're the two methods we could use to remove data from local storage, remove item to remove a
single item, and clear to remove all the items that we've previously set.