Web Storage
Web Storage provides two mechanisms for storing key/value pairs in the browser: localStorage (persists until cleared) and sessionStorage (cleared when tab closes).
Both store strings only and have a capacity of ~5MB per origin.
Web Storage provides two mechanisms for storing key/value pairs in the browser: localStorage (persists until cleared) and sessionStorage (cleared when tab closes).
Both store strings only and have a capacity of ~5MB per origin.
// localStorage — persists across sessions
localStorage.setItem("user", "Alice");
console.log(localStorage.getItem("user")); // "Alice"
// sessionStorage — cleared when tab closes
sessionStorage.setItem("tempId", "xyz123");
// Both share the same API
// localStorage.length, .key(), .setItem(), .getItem(), .removeItem(), .clear()
Never store sensitive data (passwords, tokens, PII) in localStorage — it is accessible to any JavaScript on the page.
More in JavaScript