Skip to main content

Featured

🏁 Day 45 — JavaScript Final Review

🏁 Day 45 — JavaScript Final Review Congratulations, Guys 🎉 You’ve successfully completed the 45 Days JavaScript Full Course ! From HTML DOM manipulation to APIs, localStorage, asynchronous programming, and projects — you’ve covered the full journey from beginner to confident front-end developer. 🧠 1️⃣ What You’ve Mastered 📚 JavaScript Fundamentals — variables, loops, data types, operators. 🧩 DOM Manipulation — selecting, modifying, and dynamically updating HTML. ⚙️ Functions, Events, and Scope — the logic backbone of all programs. 🌐 Fetch API & JSON — fetching real-world data and displaying it dynamically. 💾 Web Storage — localStorage, sessionStorage, and cookies. 🎨 UI Integration — working with Bootstrap and interactive interfaces. 💬 Real Projects — Quiz App, Weather Dashboard, and Chat Simulation. Each of these topics reflects real-world JavaScript usage. If you can explain and code these concepts — you’re job-ready! 📦 2️⃣ Final JavaScript R...

💾 Day 39 — Web Storage APIs: localStorage, sessionStorage & Cookies

💾 Day 39 — Web Storage APIs: localStorage, sessionStorage & Cookies

Hi Guys Welcome to Day 39 🎯 You’ve fetched and displayed data, but what if you want to store user information directly inside the browser — without needing a backend or database? That’s where the Web Storage APIs come in: localStorage, sessionStorage, and Cookies.

📦 1️⃣ Why Store Data in the Browser?

Client-side storage allows your app to:

  • 💡 Save preferences like dark mode or language.
  • 🧮 Cache data for offline use.
  • ⚡ Avoid unnecessary API calls.
  • 🔐 Maintain login sessions (with tokens).

📁 2️⃣ localStorage — Permanent Storage

localStorage stores data with no expiration date — it stays until you manually clear it. Stored values persist even after closing or reopening the browser.

// Save data
localStorage.setItem("username", "Rahul");

// Retrieve data
const name = localStorage.getItem("username");
console.log(name);

// Remove one item
localStorage.removeItem("username");

// Clear everything
localStorage.clear();

✅ Stores strings only — use JSON.stringify() for objects.

const user = { name: "Aisha", age: 25 };
localStorage.setItem("user", JSON.stringify(user));

const storedUser = JSON.parse(localStorage.getItem("user"));
console.log(storedUser.name);

🧭 3️⃣ sessionStorage — Temporary Storage

sessionStorage is similar but lives only for a browser tab session. Once the tab is closed, the data disappears.

sessionStorage.setItem("activeTab", "Dashboard");
console.log(sessionStorage.getItem("activeTab"));

Use this for temporary information like current page state or ongoing quiz progress.

🍪 4️⃣ Cookies — Old but Still Useful

Cookies store small pieces of data (max 4KB) and are automatically sent to the server with every HTTP request.

document.cookie = "theme=dark; expires=Fri, 31 Dec 2025 23:59:59 UTC; path=/";

console.log(document.cookie);
  • Good for authentication tokens.
  • Bad for large storage (size limit).
  • Always specify expiration to avoid clutter.

🧩 5️⃣ Comparison Table

FeaturelocalStoragesessionStorageCookies
LifetimeUntil clearedUntil tab closedSet by expiry date
Storage Limit~5MB~5MB4KB
Accessible byBrowserBrowserBrowser + Server
Best forPreferences, cacheSession dataLogin tokens

⚙️ 6️⃣ Practical Example — Dark Mode Toggle

const toggleBtn = document.getElementById("toggleMode");
let currentMode = localStorage.getItem("mode") || "light";

document.body.className = currentMode;
toggleBtn.innerText = currentMode === "light" ? "🌙 Dark" : "☀️ Light";

toggleBtn.addEventListener("click", ()=>{
  currentMode = currentMode === "light" ? "dark" : "light";
  document.body.className = currentMode;
  localStorage.setItem("mode", currentMode);
  toggleBtn.innerText = currentMode === "light" ? "🌙 Dark" : "☀️ Light";
});

🧠 7️⃣ Best Practices

  • Always convert objects using JSON.stringify().
  • Don’t store sensitive info like passwords.
  • Use cookies only for authentication or tracking.
  • Clear old keys periodically.

🎯 Summary

You’ve learned how to save, retrieve, and manage data directly in the browser. Now, it’s time to use these skills in a hands-on project — building a Weather Dashboard that fetches live data and stores user preferences!

Comments