JavaScript: localStorage
localStorage is a small browser-side notebook: values remain available after a page is closed and opened again.
What you will learn
- How to save and read a string in the browser
- How to remove a value when it is no longer needed
- Why passwords and other secrets must not be stored there
Minimal example
localStorage.setItem('theme', 'dark');
const theme = localStorage.getItem('theme');
console.log(theme); // "dark"
localStorage.removeItem('theme');Use setItem to save and getItem to read. Values are strings, so convert objects with JSON when necessary.