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
localStorageandsessionStorage.
- 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 akey/value pair of strings.
javascript
Reading Data
You retrieve the data using the key.
javascript
Removing Data
You can remove a single item, or clear the entire storage.
javascript
4. Mini Project: Dark Mode Saver
Let's build a dark mode toggle that *remembers* your choice even if you refresh the page!
html
5. MCQs
Q1: What happens to data stored insessionStorage 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!