← JavaScript Essentials
DOM and browser interaction · 15 MIN

Local storage and browser state

Local storage and browser state

localStorage stores string values across browser sessions for the same origin. Convert objects to JSON before saving and parse them when reading. Do not store passwords, secrets or sensitive personal information in browser storage.

Try it yourself

Change the example, run it, and explain the result in your own words. Then create one variation of your own.

Key idea

Use the example as a starting point, not something to memorize. Practise, inspect the result and make small improvements.

Code example

localStorage.setItem("theme", "dark");
const theme = localStorage.getItem("theme") ?? "light";