Getting Setting and Removing Items
Use the Web Storage API to read and write data.
The Storage API Surface
Both localStorage and sessionStorage expose the same four core methods: getItem(key), setItem(key, value), removeItem(key), and clear(). They are simple key-value stores backed by the browser.
setItem Stores Strings
localStorage.setItem("theme", "dark") persists a string under the key "theme". Both the key and value must be strings — non-string values are coerced via String() before storage, which silently corrupts objects and arrays.
localStorage.setItem("theme", "dark");
localStorage.setItem("count", "42");All lessons in this course
- localStorage vs sessionStorage
- Getting Setting and Removing Items
- Listening for Storage Events
- Security What Never to Store in Storage