Local Storage Vs Session Storage in JavaScript

Local Storage Vs Session Storage in JavaScript

Hello Guuys!

Today we'll be taking a loot two web API and their differences,

## Local Storage

// set the value in local storage object
localStorage.setItem("name", myName);

// get the value from storage
localStorage.getItem("name");

// delete the value from local storage
localStorage.removeItem("name");

// clear all the value from local storage
localStorage.clear();

**## Session Storage **

// set the value in session storage object
sessionStorage.setItem("name", myName);

// get the value from storage
sessionStorage.getItem("name");

// delete the value from session storage
sessionStorage.removeItem("name");

// clear all the value from session storage
sessionStorage.clear();

So what's the difference ?

Local Storage:

  • 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 Rebot too.

Session Storage:

  • The sessions storage 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.