What is the difference between SessionStorage and LocalStorage?
A short talk about disk space and how much storage can you actually have in your HTML5 localStorage object. The current suggested amount is up to 2MB.
Video Transcript
00:00In the last few videos, we learned everything you really need to know about local storage.
00:04It's time for us to explore the session storage, and session storage is really easy.
00:08We're not learning anything new beyond understanding the difference between local storage and session storage.
00:14So let's start.
00:15First of all, our local storage basically saves its information, and it's there forever, or it's there until you delete it.
00:21So in this case, if we go into, and by the way, this stipulation to that is only if the browsers change out and decide that it's in a different way,
00:28but currently, the way it's configured, you save that, you go back to it in a year from now, which it could still be there.
00:34All right, what I want to do is I want to create the session storage before I even explain what it is.
00:38All I'm going to do is I'm going to go to the, I copied the same example from our local storage,
00:43and I'm just going to do a search and replace, and I'm going to change the value in my search and replace,
00:48and change it from local storage to session storage.
00:51I don't know if you could see that on your screen right now, so let me make it smaller,
00:55and now you can see that I'm going to change it from local storage to session storage.
00:59All right, and I'm going to click on replace all, and now all of our session storage has been changed to be session.
01:06Next, I'm just going to make sure that I'm not deleting and removing elements anymore,
01:09so I'm just going to get rid of that clear, and I'm going to get rid of that remove item for our session storage,
01:15and I'm going to do the same for our local storage, so our examples are similar, and we could actually see them and compare them.
01:21All right, back into our browser, now that we have everything changed to what we needed to be changed,
01:26if we go back into our browser, we will see in our browser that first of all,
01:30if I click on to my local storage, and I click on refresh, and just remember the value here,
01:35all right, so we're at two for our local storage.
01:38And on the other hand, I'm going to click on refresh from my session storage,
01:42and we're going to see that currently we're in a session that has been going on for a while,
01:45so let me open up a new window, I close my session storage, all right,
01:50and I'm going to put inside of my document our session storage, drop it right into the new window of where our local data was,
01:58and notice how the session storage is resetting, going back to one,
02:02and if I click on refresh, now it's going to be two, now it's going to be three,
02:07if I open up another window, we'll see that if I pop in our session storage again, you'll see that it's one.
02:13What, did it reset just because we opened up a window? No, actually it's bounded to the specific window that you're in,
02:20in other words, all the information that you're storing here is specifically for that window,
02:24this makes it incredibly useful, for example, in scenarios where you have maybe a comparison site,
02:30or a site where you're doing two different things at the same time,
02:33and you want to store the same information in it.
02:35So let's say every window could only have one comparison between two items,
02:40but you could have two windows up at the same time,
02:43and they could use the same logic without making the logic more complex.
02:47Alright, so basically it enables you to do a lot more things and much more ease,
02:51and the session storage automatically gets deleted as soon as you close your window.
02:55Contrary to that, your local storage, even if you close it, open it up again later,
02:59your local storage will not be deleted, you could get to four there, open it up again,
03:03and another window you'll see will move into five, and the local storage will remain there forever.
03:09While contrary to that, on the other hand, whenever you open up a new window for the session storage,
03:13the session storage will reset.
03:15Although if you're opening the same thing in the same window, it will be the same session.
03:19But if you open up a new window, then that new window,
03:22whenever you put in a new session in it, is going to be a completely new session,
03:26and it's going to start from scratch again, making it completely, completely useful,
03:31and very, very convenient for a lot of different types of site,
03:34and it reduces the amount of stress that a server needs to have,
03:38because the server doesn't need to worry about this information anymore,
03:41and it could be directly applied onto the client side.
03:44Alright, so that's all you have to do to know about local storage and session storage
03:48to understand the differences between them,
03:51and to understand when do you want to use local storage,
03:54and when would you want to use a session storage instead of that.
