DEV Community

sabrinasuarezarrieta
sabrinasuarezarrieta

Posted on • Updated on

LocalStorage and SessionStorage cheat sheet

LocalStorage and SessionSorage are the features that support current browsers thanks to HTML 5 to save information on the client-side. We keep this information like the behaviour of a dictionary, that is, Key and Value. Everything saved turns out to be a string. Unlike cookies, this information is only persisted on the client-side and is never sent in each request made by our application.

LocalStorage

  • Data is shared between all tabs and windows from the same origin.
  • The data will not expire. It will remain even after browser restart and survive OS reboot too.
  • Limits the size of data you can store (~5MB across all major browsers).
//Set the value in a local storage object
localStorage.setItem('name', myName);

//Get the value from storage object
localStorage.getItem('name');

//Delete the value from local storage object
localStorage.removeItem(name);//Delete specifice obeject from local storege
localStorage.clear();//Delete all from local storege
Enter fullscreen mode Exit fullscreen mode

SessionStorage

  • The sessionStorage exists only within the current browser tab. Another tab with the same page will have different session storage.
  • It is shared between iframes in the same tab (assuming they come from the same origin).
  • The data survives page refresh, but not closing/opening the tab.
  • Limits the size of data you can store (5MB to 10MB).
//Set the value in a session storage object
sessionStorage.setItem('name', myName);

//Get the value from storage object
sessionStorage.getItem('name');

//Delete the value from session storage object
sessionStorage.removeItem(name);//Delete specifice obeject from local storege
sessionStorage.clear();//Delete all from session storage
Enter fullscreen mode Exit fullscreen mode

Finally, this is a cheat sheet but I have to clarify something ... unless you need to save information that

  • Is not at all sensitive
  • Doesn’t need to be used in an ultra-high-performance app
  • Isn’t larger than 5MB.

I don't recommend you to use any of this storages, and I will encourage you to read this post about it, because there are better options out there for your necessities.

Please Stop Using Local Storage

I wish you all lots of success and thank you for reading!!!

Top comments (0)