Skip to main content

Lesson 1 of 9

Web Storage API

HTML5 Web Storage (localStorage and sessionStorage) lets you store key-value data in the browser without cookies. localStorage persists forever (until cleared); sessionStorage clears when the tab closes. Both store up to 5-10MB per origin - far more than cookies' 4KB limit.

30 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

localStorage vs sessionStorage vs Cookies

  • localStorage - Persists forever across sessions. 5-10MB. Not sent to server. Same-origin only
  • sessionStorage - Cleared when tab/window closes. 5-10MB. Isolated per tab. Good for temp data
  • Cookies - Sent with every HTTP request. 4KB limit. Can set expiration. Used for authentication tokens
  • All store strings only - use JSON.stringify() to store objects, JSON.parse() to retrieve
  • Same-origin policy - storage is isolated per protocol + domain + port
  • No encryption - never store sensitive data (passwords, tokens) in Web Storage

Web Storage Code

Web Storage Code
// localStorage - persists forever
localStorage.setItem('username', 'Alice');
localStorage.setItem('theme', 'dark');

const user = localStorage.getItem('username'); // 'Alice'
localStorage.removeItem('username');
localStorage.clear(); // removes everything

// Storing objects (must serialize)
const settings = { theme: 'dark', fontSize: 16, language: 'en' };
localStorage.setItem('settings', JSON.stringify(settings));
const saved = JSON.parse(localStorage.getItem('settings'));

// sessionStorage - cleared on tab close
sessionStorage.setItem('cartTotal', '49.99');

// Listen for storage changes (cross-tab sync)
window.addEventListener('storage', (event) => {
  console.log(`Key: ${event.key}`);
  console.log(`Old: ${event.oldValue}`);
  console.log(`New: ${event.newValue}`);
});

Warning

Never store sensitive data like passwords, API keys, or authentication tokens in localStorage or sessionStorage. Both are accessible via JavaScript - any XSS vulnerability exposes all stored data. Use HttpOnly cookies for session tokens and server-side storage for sensitive information.

Diagram
Loading diagramโ€ฆ

localStorage persists forever; sessionStorage clears when the tab closes

Try It Yourself: Theme Switcher

Try It Yourself: Theme SwitcherHTML
HTML
โœ“ Valid
HTML|46 lines|โœ“ Valid syntax
UTF-8

Quick Quiz: Web Storage

Practice Task: Build a Persistent Notes App

  • Create a simple notes app with a <textarea> and a Save button
  • On clicking Save, store the textarea content in localStorage with a key like 'myNotes'
  • On page load, check if 'myNotes' exists in localStorage and pre-fill the textarea with the saved content
  • Add a Clear button that removes the saved notes from localStorage and empties the textarea
  • Bonus: Display a 'Last saved: [timestamp]' message using a second localStorage key

Key Takeaways

  • HTML5 Web Storage (localStorage and sessionStorage) lets you store key-value data in the browser without cookies.
  • localStorage - Persists forever across sessions. 5-10MB. Not sent to server. Same-origin only
  • sessionStorage - Cleared when tab/window closes. 5-10MB. Isolated per tab. Good for temp data
  • Cookies - Sent with every HTTP request. 4KB limit. Can set expiration. Used for authentication tokens

Finished this lesson?

0 of 9 in Module 8
Sign in to save progress

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/9 done