0Pricing
HTML Academy · Lesson

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

  1. localStorage vs sessionStorage
  2. Getting Setting and Removing Items
  3. Listening for Storage Events
  4. Security What Never to Store in Storage
← Back to HTML Academy