DEV Community

Cover image for πŸŒπŸ”—HTML Web Storage API
Himanay Khajuria
Himanay Khajuria

Posted on

πŸŒπŸ”—HTML Web Storage API

✎ᝰ INTRODUCTION

Web Storage API allows websites to store data in the browser, making web apps faster and more efficient. Let's dive deep into Local Storage and Session Storage with examples, best practices and advanced features!


πŸ“ What is Web Storage API?

The Web Storage API is a JavaScript API that allows websites to store small amounts of data directly in the browser. The stored data is:

βœ… Persistent (Local Storage) or temporary (Session Storage)

βœ… Faster than traditional cookies

βœ… Stored in key-value pairs


πŸ“– Types of Web Storage

πŸ”₯ Feature πŸ—ƒοΈ Local Storage πŸ•’ Session Storage
Lifespan Forever until deleted Only during the session
Cleared on? Manual deletion by user Closing the tab/window
Scope Available across all tabs/windows Available only in the current tab
Storage Limit ~5MB per origin ~5MB per origin
Accessibility Accessible from all browser windows Limited to the current tab

πŸ‘‡ Step-by-Step Guide with Examples

🏷️ 1. Local Storage (Persistent Data):

Local Storage keeps data even after the browser is closed.

🧩 Example: Storing, retrieving, and deleting data in Local Storage

// βœ… Store data localStorage.setItem('theme', 'dark'); // βœ… Retrieve data let theme = localStorage.getItem('theme'); console.log(theme); // Output: dark // ❌ Remove a single item localStorage.removeItem('theme'); // πŸ”„ Clear all storage localStorage.clear(); 
Enter fullscreen mode Exit fullscreen mode

πŸ”„ 2. Session Storage (Temporary Data):

Session Storage keeps data only while the browser tab is open.

🧩 Example: Storing, retrieving, and deleting data in Session Storage

// βœ… Store data sessionStorage.setItem('authToken', 'xyz123'); // βœ… Retrieve data let token = sessionStorage.getItem('authToken'); console.log(token); // Output: xyz123 // ❌ Remove a single item sessionStorage.removeItem('authToken'); // πŸ”„ Clear all storage sessionStorage.clear(); 
Enter fullscreen mode Exit fullscreen mode

🎭 Advanced Feature: Storing Objects in Web Storage

By default, Web Storage only supports strings. To store objects, use JSON.stringify() and JSON.parse().

🧩 Example: Storing and retrieving an object

// βœ… Storing an object in Local Storage let user = { name: "Alice", age: 25 }; localStorage.setItem('user', JSON.stringify(user)); // βœ… Retrieving the object let storedUser = JSON.parse(localStorage.getItem('user')); console.log(storedUser.name); // Output: Alice 
Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ Debugging Web Storage in Browser

You can view and manage Web Storage using Developer Tools:

1️⃣ Open DevTools β†’ Press F12 or Ctrl + Shift + I

2️⃣ Go to Application Tab

3️⃣ Expand Storage β†’ Local Storage / Session Storage

4️⃣ View, edit, or delete stored data

πŸ–ΌοΈ Example Screenshot:

Click to Watch


⚑ Best Practices for Web Storage

βœ”οΈ Do NOT store sensitive information (e.g., passwords, tokens)
βœ”οΈ Use JSON for complex data
βœ”οΈ Keep storage size minimal (Limit is ~5MB per origin)
βœ”οΈ Clear old data periodically to free up space
βœ”οΈ Use Web Storage for temporary and non-critical data


πŸ“ When to Use Web Storage?

βœ… Good Use Cases:
βœ”οΈ Theme preferences (dark/light mode)
βœ”οΈ User-selected language settings
βœ”οΈ Caching API responses (temporary)
βœ”οΈ Shopping cart data (before checkout)

❌ Avoid Web Storage for:
🚫 Sensitive user data (use server-side storage instead)
🚫 Large data sets (use IndexedDB for larger data)


πŸ”₯ Web Storage vs. Cookies - What's the Difference?

Feature πŸͺ Cookies πŸͺ Web Storage
Storage Limit ~4KB ~5MB
Data Expiry Manually set LocalStorage: Forever, SessionStorage: Until tab closes
Sent with Requests? Yes (sent to server) No
Security Less secure More secure (not automatically shared with the server)
Use Case Authentication, tracking Storing non-sensitive user settings

➑ Conclusion β¬…

πŸ’‘ The Web Storage API is a powerful tool for storing client-side data. Whether you need persistent storage (Local Storage) or session-based storage (Session Storage), the Web Storage API makes it easy to manage data efficiently.

βœ… Use Local Storage for long-term data

βœ… Use Session Storage for temporary data

βœ… Use JSON for structured data


🎁 Bonus: Quick Cheatsheet

// βœ… Local Storage localStorage.setItem('key', 'value'); // Store localStorage.getItem('key'); // Retrieve localStorage.removeItem('key'); // Remove item localStorage.clear(); // Clear all // βœ… Session Storage sessionStorage.setItem('key', 'value'); sessionStorage.getItem('key'); sessionStorage.removeItem('key'); sessionStorage.clear(); 
Enter fullscreen mode Exit fullscreen mode

πŸ–‡οΈ Want to Learn More?

βž₯ Check out my Technical Presentation along with the slide notes for a better understanding πŸŽ₯: πŸ”— View Presentation
βž₯ MDN Web Docs - Web Storage API
βž₯ Web Storage Guide on W3Schools

πŸ’Ό Let’s connect on LinkedIn! πŸ‘₯πŸ’¬

β™‘ Happy Coding! πŸ€— β™‘

Top comments (0)