codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎨

HTML & CSS

16 / 59 topics
11HTML Semantic Elements12HTML5 New Elements13HTML5 Forms14HTML5 Media Elements15HTML5 Canvas16HTML5 Web Storage
Tutorials/HTML & CSS/HTML5 Web Storage
🎨HTML & CSS

HTML5 Web Storage

Updated 2026-04-20
2 min read

Introduction

HTML5 introduced two new mechanisms for client-side data storage: Web Storage and IndexedDB. In this tutorial, we will focus on Web Storage, which includes two types of storage: localStorage and sessionStorage. These storage options provide a way to store key-value pairs in the browser without requiring a server.

Overview of Web Storage

localStorage

  • Persistence: Data stored in localStorage has no expiration time. It persists even after the browser window is closed.
  • Scope: Data is available across all tabs and windows with the same origin (protocol, domain, and port).
  • Capacity: Typically around 5-10MB per origin.

sessionStorage

  • Persistence: Data stored in sessionStorage is cleared when the tab or browsing context is closed.
  • Scope: Data is only available within the tab it was created in.
  • Capacity: Similar to localStorage, typically around 5-10MB per origin.

Basic Operations

Storing Data

To store data, use the setItem() method. This method takes two parameters: a key and a value (which must be a string).

// Storing data in localStorage
localStorage.setItem('username', 'JohnDoe');

// Storing data in sessionStorage
sessionStorage.setItem('theme', 'dark');

Retrieving Data

To retrieve data, use the getItem() method. This method takes one parameter: the key.

// Retrieving data from localStorage
const username = localStorage.getItem('username');
console.log(username); // Output: JohnDoe

// Retrieving data from sessionStorage
const theme = sessionStorage.getItem('theme');
console.log(theme); // Output: dark

Removing Data

To remove a specific item, use the removeItem() method. To clear all items, use the clear() method.

// Removing a specific item from localStorage
localStorage.removeItem('username');

// Clearing all items in sessionStorage
sessionStorage.clear();

Best Practices

  1. Data Types: Since Web Storage only supports strings, always convert non-string data types to JSON before storing them.
  2. Error Handling: Always handle potential errors when working with Web Storage, such as exceeding storage limits or encountering unsupported browsers.
  3. Security: Be cautious about storing sensitive information in client-side storage. Use HTTPS and consider encrypting data if necessary.

Example: Storing and Retrieving JSON Data

// Storing an object in localStorage
const user = { name: 'JohnDoe', age: 30 };
localStorage.setItem('user', JSON.stringify(user));

// Retrieving the object from localStorage
const storedUser = JSON.parse(localStorage.getItem('user'));
console.log(storedUser); // Output: { name: 'JohnDoe', age: 30 }

Events

Web Storage provides an event called storage that is triggered when a storage area (either localStorage or sessionStorage) changes in another document with the same origin.

window.addEventListener('storage', function(event) {
    console.log(`Key changed: ${event.key}`);
    console.log(`Old value: ${event.oldValue}`);
    console.log(`New value: ${event.newValue}`);
});

Use Cases

  1. User Preferences: Storing user preferences like theme, language, or font size.
  2. Session Management: Maintaining session data without server-side cookies.
  3. Offline Applications: Caching data for offline use.

Conclusion

HTML5 Web Storage provides a powerful and flexible way to store client-side data in the browser. By understanding the differences between localStorage and sessionStorage, and following best practices, you can effectively manage user data and enhance the functionality of your web applications.

Additional Resources

  • MDN Web Docs - Web Storage API
  • Can I use localStorage?

By leveraging these features, you can create more interactive and efficient web applications that provide a better user experience.


PreviousHTML5 CanvasNext Introduction to CSS

Recommended Gear

HTML5 CanvasIntroduction to CSS