mc0de
Advanced HTML · Lesson 10 of 13

Local Storage

What if your website could remember things? Learn how to save information in the browser and build apps that keep their data even after the page is refreshed.

Web Storage APIJavaScript Persistent DataInteractive App
● ● ●   local-storage.html
localStorage.setItem(
  "myNote",
  "Keep learning!"
);

const note =
  localStorage.getItem("myNote");
📝 My Saved Note Keep learning, keep building, and never stop experimenting!
✓ Saved in your browser
01 / Getting Started

How Do Websites Remember Things?

Have you ever returned to a website and found that it remembered your theme, preferences, or progress? Sometimes that information is stored directly in your browser.

localStorage is part of the Web Storage API. It lets JavaScript save small amounts of data as key-value pairs for a website’s origin.

Unlike ordinary JavaScript variables, stored values can remain available after a page reload or browser restart, until they’re cleared.

Important: localStorage isn’t a database or a secure place for passwords and private secrets. It’s best for small, non-sensitive preferences and simple application data.
02 / Saving Data

Use setItem()

To save a value, use localStorage.setItem() with a key and a value.

Save a favorite color
localStorage.setItem("favoriteColor", "cyan");

The key favoriteColor identifies the value. If you save another value using the same key, it replaces the old value.

Update an existing value
localStorage.setItem("favoriteColor", "green");

Values are stored as strings. For objects or arrays, you’ll normally use JSON.stringify() before saving them.

03 / Reading Data

Use getItem()

To retrieve a saved value, use localStorage.getItem().

Retrieve a saved color
const color = localStorage.getItem("favoriteColor");
console.log(color);

If the key doesn’t exist, getItem() returns null. You can use that to provide a default value.

Use a default value
const color =
  localStorage.getItem("favoriteColor") || "blue";
04 / Removing Data

Delete One Item or Clear Everything

Use removeItem() to delete a particular saved value.

Remove a saved item
localStorage.removeItem("favoriteColor");

There is also localStorage.clear(), which removes all localStorage entries for the current origin.

Be careful: Avoid using clear() in a shared website application. It can erase unrelated saved settings, including learning progress. Use removeItem() with your application’s own keys instead.
05 / Try It Yourself

Save a Note in Your Browser

Type a short note below, click Save, and refresh this page. Then click Load to retrieve what you saved.

Ready to save your first note!

Your note is saved only in this browser’s storage for this website. It isn’t submitted to a server by this example.

06 / Saving More Complex Data

Store Arrays and Objects with JSON

Sometimes you want to store more than a simple word or sentence. You might have a list of tasks or several user preferences.

Save and retrieve an object
const settings = {
  theme: "dark",
  fontSize: 16
};

localStorage.setItem(
  "mySettings",
  JSON.stringify(settings)
);

const saved = JSON.parse(
  localStorage.getItem("mySettings") || "{}"
);

console.log(saved.theme);

JSON.stringify() converts data into a string, while JSON.parse() converts valid JSON text back into JavaScript data.

Developer tip: Stored data can be missing or invalid. In real applications, use error handling around storage operations and JSON parsing.
07 / Storage Best Practices

Save Responsibly

  • Use descriptive, application-specific keys to avoid collisions.
  • Store only small amounts of non-sensitive information.
  • Don’t assume storage will always be available; browser settings may restrict it.
  • Remember that localStorage is synchronous and can block the main thread for large operations.
  • Provide an easy way for visitors to remove their saved data.
  • Never place passwords, authentication tokens, or confidential information in localStorage.
  • Remember that storage belongs to the current origin and browser profile, not automatically to every device.

For larger or more structured offline data, developers often use technologies such as IndexedDB.

🚀 mC0de Challenge

Build a Personal Notes App

Create a simple application that lets visitors write a note and save it in their browser. The note should still be available when they return to the page.

Your Mission

  1. Create a heading and a textarea for writing notes.
  2. Add a Save button that uses setItem().
  3. Add a Load button that uses getItem().
  4. Add a Delete button that uses removeItem().
  5. Display helpful status messages after each action.
  6. Automatically load an existing note when the app starts.
  7. Use a unique storage key for your application.
Extra challenge: Add a character counter or an automatic save feature whenever the note changes.
💻 Interactive HTML Sandbox

Build Your Notes App!

Edit the HTML and JavaScript, then click Run Code. The live preview can save notes while it stays open. For reliable persistence testing across page visits, copy your finished code into a regular page on your own site.

Live Result
Sandbox note: For safety, this editor runs in an isolated iframe. Browsers may block persistent storage inside that isolated preview. The lesson’s practice note above demonstrates actual website localStorage persistence.
✓ What You’ve Learned

Your Website Can Remember!

You’ve learned how to save information in the browser and create more useful interactive applications.

  • How localStorage stores key-value pairs.
  • How to save, read, update, and remove stored data.
  • How to store objects using JSON.
  • How to handle missing or unavailable storage.
  • How to build a simple persistent notes app.

Next up: Offline Web Apps & Service Workers — discover how modern websites can keep working when the network is unavailable!

Your Learning Journey

0 / 13 Completed

Keep going! Every completed lesson brings you closer to mastering Advanced HTML.

🎉

Nice Work!

You've learned how to save, retrieve, update, and remove browser data using localStorage. You've also explored how to build applications that remember information between visits!