OnlineCBT Logo OnlineCBT
HTML Tutorial
HTML · LESSON 60

Web Storage API Basics

By Ratan Updated: 16 Oct 2026

Store and retrieve client-side data in the browser using localStorage and sessionStorage.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

1. What is the Web Storage API?

Before HTML5, web applications stored client-side data exclusively in HTTP cookies. The Web Storage API provides a much larger (up to 5-10MB), secure, and performance-friendly mechanism to store key-value string data directly in the browser without transmitting it with every HTTP request.

2. localStorage vs sessionStorage

Feature localStorage sessionStorage
Persistence Persists indefinitely until cleared by user or script. Cleared automatically when the browser tab is closed.
Scope Shared across all tabs/windows of the same domain origin. Isolated to the specific individual browser tab.
Best For User theme preferences, auth tokens, cached questions. Active test session answers, temporary scratchpad notes.

3. Web Storage API Methods

// 1. Storing data (strings only)
localStorage.setItem("cbt_student_name", "Ankit Sharma");

// Storing objects by serializing with JSON.stringify()
const preferences = { theme: "dark", fontSize: 16 };
localStorage.setItem("cbt_prefs", JSON.stringify(preferences));

// 2. Retrieving data
const student = localStorage.getItem("cbt_student_name");
const parsedPrefs = JSON.parse(localStorage.getItem("cbt_prefs"));

// 3. Removing items & clearing
localStorage.removeItem("cbt_student_name");
localStorage.clear(); // Clears all keys for current origin

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat