Web Storage: setItem()

The setItem() method saves a string value under a key in localStorage or sessionStorage.

localStorage.setItem('language', 'en'); const language = localStorage.getItem('language');

Keys and values are strings. Convert structured data with JSON.stringify() and parse it carefully when reading.

Common mistakes