JavaScript theorytheory 0/50 · 0%
Browser · medium

41. Browser storage and state

localStorage, sessionStorage and what not to keep.

`localStorage` persists per origin until cleared; `sessionStorage` lasts for the tab. Both store strings only, are synchronous, and are readable by any script on the origin.

localStorage.setItem("progress", JSON.stringify(state));
const state = JSON.parse(localStorage.getItem("progress") ?? "{}");

Never store private keys, seed phrases or bearer tokens there. Wrap access in try/catch — private modes can throw.

Check your understanding

  1. 1. What types can be stored?

  2. 2. What must never go in localStorage?

  3. 3. Why wrap access in try/catch?