Difference between localStorage, sessionStorage and cookies

Different client side storage solutions. localStorage, sessionStorage and cookies
Different client side storage solutions. localStorage, sessionStorage and cookies

localStorage, sessionStorage and cookies are all client storage solutions which store data as key/value pairs in the browser. localStorage and sessionStorage both extend Storage. There is no difference between them except for the intended “non-persistence” of sessionStorage.

  • sessionStorage maintains a separate storage area for each given origin that’s available for the duration of the page session (as long as the browser is open, including page reloads and restores)
  • localStorage does the same thing, but persists even when the browser is closed and reopened.

both the above storages can be accessed using global variables sessionStorage and localStorage

Cookie on the other hand is used to store data that has to be sent back to the server with subsequent requests.  Cookies are primarily for reading server-side, localStorage and sessionStorage can only be read on the client-side.