Web Storage

Web Storage lets a page save small string values in the browser for later use.

What you will learn

Two kinds of storage

localStorage
Data remains for the site until it is removed, even after the browser is closed.
sessionStorage
Data belongs to the current tab session and is removed when that session ends.

Basic example

localStorage.setItem('theme', 'dark');
const theme = localStorage.getItem('theme');
localStorage.removeItem('theme');

Storage values are strings. Convert JSON explicitly when you need to save an array or object, and handle unavailable storage or quota errors.

Security and privacy