Skip to main content
HTML for Beginners
CHAPTER 19 Beginner

HTML Local Storage and Session Storage

Updated: May 10, 2026
5 min read

# HTML Local Storage and Session Storage

1. Introduction

Before HTML5, application data had to be stored in cookies, which were slow, insecure, and limited in size. The HTML Web Storage API allows web applications to store data locally within the user's browser securely and quickly, up to 5MB!

2. Learning Objectives

  • Differentiate between localStorage and sessionStorage.
  • Save, read, and delete data using the Storage API.
  • Use storage to save user preferences.

3. Detailed Explanations

Local Storage vs Session Storage

  • localStorage: Data is saved *forever* (until the user manually clears their browser cache). Good for dark-mode preferences or high scores.
  • sessionStorage: Data is saved only for the current session. If the user closes the browser tab, the data is deleted. Good for sensitive forms.

Saving Data

Data is always saved as a key/value pair of strings.
javascript
12345
// Save data to localStorage
localStorage.setItem("username", "JaneDoe");

// Save data to sessionStorage
sessionStorage.setItem("draft_email", "Hello, I wanted to say...");

Reading Data

You retrieve the data using the key.
javascript
123
// Get data
let user = localStorage.getItem("username");
console.log(user); // Outputs: "JaneDoe"

Removing Data

You can remove a single item, or clear the entire storage.
javascript
12345
// Remove one item
localStorage.removeItem("username");

// Clear EVERYTHING
localStorage.clear();

4. Mini Project: Dark Mode Saver

Let's build a dark mode toggle that *remembers* your choice even if you refresh the page!
html
123456789101112131415161718192021222324
<style>
  body.dark-mode { background-color: #121212; color: white; }
</style>

<button onclick="toggleTheme()">Toggle Dark Mode</button>

<script>
  // When the page loads, check if the user previously chose dark mode
  if (localStorage.getItem("theme") === "dark") {
      document.body.classList.add("dark-mode");
  }

  function toggleTheme() {
      // Toggle the class
      document.body.classList.toggle("dark-mode");
      
      // Save the new preference
      if (document.body.classList.contains("dark-mode")) {
          localStorage.setItem("theme", "dark");
      } else {
          localStorage.setItem("theme", "light");
      }
  }
</script>

5. MCQs

Q1: What happens to data stored in sessionStorage when the user closes the browser tab? A) It is saved forever. B) It is deleted automatically. C) It is sent to the server. D) It is moved to localStorage. *Answer: B*

6. Summary

The Web Storage API (localStorage and sessionStorage) is the easiest way to save state on the frontend. Use it to improve UX by saving unsaved form drafts, theme preferences, and cart items without requiring a backend database!

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·