LocalStorage getItem and setItem methods
There are a few ways to access a localStorage or any type of storage object: we've seen how to do the first in the last video, in this one we will see how to add and get items using the setItem and getItem methods.
Video Transcript
00:00Alright so in the last video we got the basics of how to work with local storage and it's
00:04literally just as easy, there's nothing more special about it that you really need to know.
00:08But let's look into the actual methods, how you could interact with the API and actually
00:13work with the API instead of working directly with the components.
00:17So I'm just going to take our local storage information here and I'm going to put it
00:22inside of our if statement, while you were in here I added a curly bracket as I was
00:27experimenting around.
00:29So let's look at it, so first of all what I want to do is I want to create a new local
00:32storage property or set a new value to our property and I could use the set item.
00:38Set item works the exact same way as just approaching the item directly, the property
00:42directly and putting information in it.
00:44And we could put in here whatever we want, so maybe this will be welcome back Ben, because
00:51now we know that Ben is there, let's say.
00:54Alright so we have our local storage, we're setting our item, welcome back Ben, we could
00:58save this information, as soon as we do that we could literally go back into our
01:02browser, click on refresh first time around, I don't know if we'll see the
01:05information or not, we won't, but second time around when we click on refresh
01:09we'll see welcome back Ben, and that is because we're updating the information
01:12after we already updated our document.
01:15Perfect, so now we know how to work with the set item, let's just look really
01:18quickly in our console and we're going to do the same thing for getting the
01:25information, so this time around local storage, get item, and this time around
01:30again I'm just going to approach the item I want to get and let's say we want to
01:33get visit count, and literally we're just setting the string to it and we get that
01:37information back and we'll be able to see that tracing out in our log file, so
01:41if we go back into our browser, click on refresh, we'll see in our log that this is
01:45visit number 17.
01:47Alright so the exact same thing that we did using properties could be done
01:51using functions using the set and the get, obviously using properties is more
01:55convenient, but in the next video we're going to look at a few properties that
01:59probably would be convenient, such as how do you actually remove a specific item and
02:04how do you remove all items, how do you reset, alright so we'll see that in the
02:08next video.
