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
- Do not assume storage is available or unlimited.
- Catch errors when a browser blocks storage or the quota is exceeded.
- Never store secrets just because the API is convenient.