mc0de
JavaScript · Lesson 16 of 17

JavaScript Local Storage

Make your websites remember! Learn how to save information in the browser, retrieve it after a refresh, and build interactive experiences that keep their data between visits.

setItem() getItem() removeItem() JSON Storage
● ● ●   storage.js
localStorage.setItem(
  "favoriteCourse",
  "JavaScript"
);

const course =
  localStorage.getItem(
    "favoriteCourse"
  );

console.log(course);
LIVE BROWSER MEMORY DEMO
> Browser memory ready. > Save a favorite course!
01 / Getting Started

What Is Local Storage?

Normally, a JavaScript variable exists only while the page is running. When the page reloads, that variable is recreated, and its previous value is lost.

localStorage is a browser feature that allows websites to store small amounts of information as key-value pairs. Unlike ordinary JavaScript variables, stored values can survive page refreshes and browser restarts.

Saving your first value
localStorage.setItem("favoriteColor", "cyan");

This creates an entry with the key favoriteColor and the value cyan.

Important: localStorage belongs to the website’s origin and browser storage context. It isn’t shared across unrelated websites or automatically synchronized across devices. Browsers may also restrict or clear it.
02 / Saving Data

Use setItem() to Remember Information

The setItem() method accepts two arguments: the name of the key and the value to save.

Saving multiple values
localStorage.setItem("username", "Alex");
localStorage.setItem("theme", "dark");
localStorage.setItem("lastLesson", "16");

If the key already exists, calling setItem() again replaces its previous value.

Updating an existing value
localStorage.setItem("theme", "light");

Storage values are strings. Even when you save a number, it will be retrieved as text.

Example
Saved: 16 Retrieved: “16” Type: string
03 / Retrieving Data

Use getItem() to Read Saved Values

Use getItem() to retrieve information by its key.

Reading saved information
const username = localStorage.getItem("username");

console.log(username); // "Alex"

If a key does not exist, getItem() returns null.

Providing a default value
const savedTheme = localStorage.getItem("theme");

const theme = savedTheme ?? "light";

console.log(theme);

The ?? operator supplies a fallback when the saved value is null or undefined.

Practical idea: A website can remember a visitor’s preferred color theme and apply it when they return.
04 / Removing Data

Delete Individual Items or Clear Storage

To delete one stored entry, use removeItem().

Removing a saved preference
localStorage.removeItem("theme");

You can also use clear() to remove every localStorage entry belonging to the current origin.

Clearing all entries for an origin
// Caution: removes all localStorage entries
// belonging to this website's origin.
localStorage.clear();

For most applications, remove only your application’s keys. Calling clear() on a shared website could erase unrelated preferences, saved notes, or learning progress.

mC0de reminder: Our lessons use localStorage to track completion. Never use localStorage.clear() in a live lesson demo, because that could erase students’ progress.
05 / Storing Objects & Arrays

Combine Local Storage With JSON

localStorage stores strings, not JavaScript objects directly. To save an array or object, first convert it into a JSON string.

Saving an array of notes
const notes = [
  { title: "JavaScript", text: "Practice every day." },
  { title: "Project", text: "Build a dashboard." }
];

localStorage.setItem(
  "myNotes",
  JSON.stringify(notes)
);

To retrieve the array, use JSON.parse().

Retrieving the notes
const saved = localStorage.getItem("myNotes");

const notes = saved ? JSON.parse(saved) : [];

console.log(notes[0].title); // JavaScript

For production code, handle malformed JSON using try...catch, since stored data might have been modified or become incompatible with a newer version of your application.

Connection to Lesson 14: JSON.stringify() converts arrays and objects into strings, while JSON.parse() reconstructs them when you retrieve the stored information.
06 / Storage Limitations & Safety

Know What Local Storage Can—and Cannot—Do

localStorage is useful for small, non-sensitive pieces of information such as interface preferences, drafts, and learning progress. But it isn’t a database replacement or a secure place for private data.

  • It is generally limited to a relatively small amount of data per origin.
  • Its methods are synchronous, so large operations can affect page responsiveness.
  • Users can delete saved information through browser settings.
  • Storage may be unavailable in some private or restricted browsing contexts.
  • Scripts running on the same origin can read its contents.
Handling storage errors
try {
  localStorage.setItem("draft", "My first note");
  console.log("Saved!");
} catch (error) {
  console.log("Browser storage is unavailable.");
}
Security: Never save passwords, authentication tokens, payment information, or sensitive personal details in localStorage. This lesson uses simple, non-sensitive demonstration notes.
07 / Local Storage in Action

Try the Live Browser Memory Demo

Enter a short message, save it, and refresh the page. Then retrieve your message to see how the browser remembers it.

Live Browser Memory Tester
No message loaded yet.

Try saving a message, reloading the page, and selecting Retrieve. Delete the message when you’re finished.

🚀 mC0de Challenge

Build a Personal Notes Manager

Create a simple notes application that remembers a visitor’s notes between page refreshes.

Your Mission

  1. Create a text field for the note title and a textarea for the note content.
  2. Add a Save Note button.
  3. Store notes as an array of objects using JSON.stringify().
  4. Load previously saved notes with JSON.parse().
  5. Display each note as a card using DOM methods.
  6. Add a Delete button to remove individual notes.
  7. Refresh the page and verify that the notes remain available.
Extra challenge: Add note editing, search, and a saved-notes counter. Use a dedicated storage key so the application doesn’t interfere with learning progress.
💻 Interactive JavaScript Sandbox

Build Your Personal Notes Manager!

The starter project lets students create, save, and delete notes. Select Load Solution to add editing, search, and a notes counter.

Live Result

Browser security may prevent localStorage inside an isolated sandbox iframe. This sandbox therefore uses a parent-page storage bridge for its own dedicated notes key. Saved notes persist when the browser permits storage, while the preview remains isolated.

Lesson 16 Recap

What You’ve Learned

You’re Building Websites That Remember!

You’ve learned how JavaScript can preserve non-sensitive information between page visits.

  • Understand what localStorage is and when to use it.
  • Save information using setItem().
  • Retrieve information using getItem().
  • Delete individual entries with removeItem().
  • Store arrays and objects with JSON.
  • Handle storage errors and understand security limitations.
  • Build a persistent Personal Notes Manager.

Next: Lesson 17 — Final Project! You’ll combine everything you’ve learned into a complete interactive JavaScript application.

Your Learning Journey

0 / 17 Completed

Lesson 16 of 17 — one final project away from completion!

🎉

You've Mastered Local Storage!

You can now save and retrieve browser data, store JavaScript objects with JSON, and build applications that remember information. Your Personal Notes Manager is complete!