DEV Community

Discussion on: Local Storage and Session Storage explained

Collapse
 
loucyx profile image
Lou Cyx

localStorage is used for all kinds of things:

  • User preferences/settings. You could save them locally to not have to retrieve them constantly, and also update them locally when the user is offline and then sync them once they regain connection.
  • User authentication tokens or session information. Not ideal (cookies might be safer for this particular thing), but there are several apps that do this.
  • Cached data or results of expensive computations. You could for example save in localStorage the last response you got from and endpoint and then when the user goes back to the same view first show the data cached in localStorage and then update it when you get the real response.
  • Other local application state. Any other local state that needs to be saved in the user device.

sessionStorage is used for stuff like...

  • Current user's shopping cart items.
  • Form data across multiple pages or steps.
  • Temporary variables or flags for use within the session.
  • Page-specific data that should not persist beyond the session.

Keep in mind that is useful to use this client side storage solutions, but only when they make sense. Avoid over-using them when you can solve the problem you're dealing with with an in-memory value, form states, and so on.

Cheers!