BCA Student Stories & UpdatesLive
Web Storage Architecture: localStorage, sessionStorage & JSON Serialization
Web Storage APIs provide a modern, secure way to store key-value pairs directly inside the client browser with up to 5MB of storage without expiring on page reload. This lesson teaches you how to serialize complex JavaScript objects and persist user choices seamlessly.
Day 31 Step-by-Step Practical Flow
🎯1. What You Will Learn Today
Web Storage Architecture: localStorage, sessionStorage & JSON Serialization — Comprehensive Practical Guide
Har concept ko visual mental models, code anatomy aur practical industry tips ke sath samjhein.
Web Storage APIs provide a modern, secure way to store key-value pairs directly inside the client browser with up to 5MB of storage without expiring on page reload. This lesson teaches you how to serialize complex JavaScript objects and persist user choices seamlessly.
"Think of localStorage like your browser backpack: whenever you close the door (tab/browser) and come back tomorrow, everything you packed inside remains exactly where you left it."
👨🏫3. Teacher Demonstration Code
See how user theme choices and profile names are serialized to localStorage and re-applied immediately upon page mount.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: 'Segoe UI', sans-serif; transition: background 0.3s, color 0.3s; padding: 24px; }
body.light { background: #f8fafc; color: #0f172a; }
body.dark { background: #060c18; color: #ffffff; }
.card { max-width: 420px; margin: auto; padding: 24px; border-radius: 14px; border: 1px solid #334155; }
button { padding: 10px 16px; border-radius: 8px; border: none; cursor: pointer; font-weight: bold; margin-top: 10px; }
.btn-toggle { background: #2563eb; color: #fff; }
input { width: 100%; padding: 10px; margin: 8px 0; border-radius: 6px; border: 1px solid #475569; box-sizing: border-box; }
</style>
</head>
<body class="dark">
<div class="card">
<h2>🌓 Theme & Storage Manager</h2>
<p id="welcomeText">Welcome, Guest!</p>
<input type="text" id="usernameInput" placeholder="Enter your name..." />
<button id="saveBtn" class="btn-toggle">Save Name to LocalStorage</button>
<button id="themeBtn" class="btn-toggle" style="background:#04aa6d;">Toggle Theme (☀️/🌙)</button>
<button id="clearBtn" style="background:#ef4444; color:#fff;">Clear Storage</button>
</div>
<script>
const themeBtn = document.getElementById("themeBtn");
const saveBtn = document.getElementById("saveBtn");
const clearBtn = document.getElementById("clearBtn");
const usernameInput = document.getElementById("usernameInput");
const welcomeText = document.getElementById("welcomeText");
// 1. Hydrate state on page load
const savedTheme = localStorage.getItem("bca_theme") || "dark";
document.body.className = savedTheme;
const savedUser = JSON.parse(localStorage.getItem("bca_user"));
if (savedUser && savedUser.name) {
welcomeText.textContent = "Welcome back, " + savedUser.name + "!";
usernameInput.value = savedUser.name;
}
// 2. Toggle and persist theme
themeBtn.addEventListener("click", () => {
const newTheme = document.body.className === "dark" ? "light" : "dark";
document.body.className = newTheme;
localStorage.setItem("bca_theme", newTheme);
});
// 3. Serialize and save object
saveBtn.addEventListener("click", () => {
const name = usernameInput.value.trim();
if (!name) return;
const userProfile = { name: name, lastLogin: new Date().toISOString() };
localStorage.setItem("bca_user", JSON.stringify(userProfile));
welcomeText.textContent = "Welcome back, " + name + "!";
});
// 4. Clear storage
clearBtn.addEventListener("click", () => {
localStorage.removeItem("bca_user");
welcomeText.textContent = "Welcome, Guest!";
usernameInput.value = "";
});
</script>
</body>
</html>2. Hands-On Practical Lab: Follow Instructions in VS Code
Code like a professional software engineer! Follow the step-by-step instructions below in Visual Studio Code (VS Code) on your PC, test your output locally, and push to GitHub.
Day 31 Practical Task Test Studio
Use localStorage.getItem("bca_student_prefs") and JSON.parse() on page load to prefill the input fields if data exists.
Assemble the input values into an object and save them using localStorage.setItem("bca_student_prefs", JSON.stringify(obj)).
The "[object Object]" LocalStorage Corruption Bug
Tap or click on the exact line below that contains the error:
Click any code lineconst profile = { name: "Aman", semester: 1 };localStorage.setItem("student_profile", profile); // Later in code...const saved = JSON.parse(localStorage.getItem("student_profile"));console.log(saved.name); // ❌ SyntaxError: Unexpected token 'o', "[object Object]" is not valid JSONSenior Developer Forensic Analysis:
localStorage only accepts strings. When passing an object directly without JSON.stringify(), JavaScript invokes profile.toString() which converts the object into the literal string "[object Object]". Attempting to JSON.parse("[object Object]") throws a fatal SyntaxError.
📝Independent Practice: Assignment
📄 index.htmlInteractive Persistent Student Profile & Study Notepad
- •Create a responsive student dashboard card with Dark/Light theme toggle.
- •Include an editable note textarea where student revision notes are auto-saved on every keystroke (input event) into localStorage.
- •Add a "Reset to Defaults" button that clears localStorage with confirm() verification.
- •Ensure the application displays an active status badge showing "Saved to LocalStorage" with a timestamp.
Open Visual Studio Code on your PC (code .), create index.html, paste this complete step-by-step verified code, test in your browser, and push to your GitHub repo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BCA Day 31 - Persistent Study Notepad</title>
<style>
body { font-family: 'Segoe UI', sans-serif; background: #060c18; color: #fff; padding: 24px; transition: all 0.3s; }
body.light { background: #f8fafc; color: #0f172a; }
.card { max-width: 540px; margin: 0 auto; background: #0c1328; border: 1px solid #1e293b; border-radius: 12px; padding: 24px; }
body.light .card { background: #ffffff; border-color: #e2e8f0; }
textarea { width: 100%; height: 140px; background: #060c18; color: #fff; border: 1px solid #334155; border-radius: 8px; padding: 12px; box-sizing: border-box; }
body.light textarea { background: #f1f5f9; color: #0f172a; border-color: #cbd5e1; }
.actions { display: flex; gap: 10px; margin-top: 14px; }
button { flex: 1; padding: 10px; border-radius: 6px; border: none; font-weight: bold; cursor: pointer; }
.btn-theme { background: #2563eb; color: #fff; }
.btn-clear { background: #ef4444; color: #fff; }
.status { font-size: 12px; color: #10b981; margin-top: 8px; font-weight: bold; }
</style>
</head>
<body class="dark">
<div class="card">
<h2>📓 BCA Persistent Study Notepad</h2>
<textarea id="notesArea" placeholder="Type your revision notes here... (auto-saved)"></textarea>
<div class="status" id="saveStatus">Synced with LocalStorage ✅</div>
<div class="actions">
<button class="btn-theme" id="themeBtn">🌓 Toggle Theme</button>
<button class="btn-clear" id="clearBtn">🗑️ Clear Notes</button>
</div>
</div>
<script>
const notesArea = document.getElementById("notesArea");
const saveStatus = document.getElementById("saveStatus");
const themeBtn = document.getElementById("themeBtn");
const clearBtn = document.getElementById("clearBtn");
// Hydrate
notesArea.value = localStorage.getItem("bca_notes") || "";
const savedTheme = localStorage.getItem("bca_theme") || "dark";
document.body.className = savedTheme;
// Auto-save on input
notesArea.addEventListener("input", () => {
localStorage.setItem("bca_notes", notesArea.value);
saveStatus.textContent = "Saved at " + new Date().toLocaleTimeString();
});
themeBtn.addEventListener("click", () => {
const nextTheme = document.body.className === "dark" ? "light" : "dark";
document.body.className = nextTheme;
localStorage.setItem("bca_theme", nextTheme);
});
clearBtn.addEventListener("click", () => {
if (confirm("Clear all notes from browser storage?")) {
localStorage.removeItem("bca_notes");
notesArea.value = "";
saveStatus.textContent = "Storage cleared.";
}
});
</script>
</body>
</html>🎓4. Viva Check: Mandatory Conceptual Test
What is the primary difference between localStorage and sessionStorage in modern browsers?
5. Mandatory Step: Push to GitHub to Unlock Next Lesson
Target Repo: html-css-projects
Exact GitHub Setup Guide & Expected Structure
Day 31 RequirementsFollow these exact folder and file names to prevent verification errors.
index.html: Avoid spaces or custom names like Alan Turing..html or Index.HTML. Linux/GitHub servers are strictly case-sensitive.day-31/ for today so you don't overwrite previous days.main.You are welcome to read all notes, inspect code snippets, and practice all exercises of Day 31 freely! To officially submit your GitHub repository and unlock subsequent days in your portfolio, please complete Day 30: Sprint 3 Capstone: Interactive Student SGPA & Grade Portal first.
Complete Day 30 First