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 32 — Maps, Sets, WeakMaps & WeakSets

🧠 Day 32 — Maps, Sets, WeakMaps & WeakSets

Hi Guys Welcome to Day 32 πŸ™Œ You’ve learned modern JavaScript syntax — now let’s explore modern **data structures**. While objects and arrays work fine for most cases, ES6 introduced **Map**, **Set**, **WeakMap**, and **WeakSet** for better performance and control over memory.

πŸ“˜ 1️⃣ Map — Key-Value Pairs (Better than Objects)

Unlike objects, Map allows any type of key — even objects or functions.

let userAge = new Map();
userAge.set("Rahul", 22);
userAge.set("Aisha", 25);

console.log(userAge.get("Rahul")); // 22
console.log(userAge.size); // 2

userAge.delete("Aisha");
console.log(userAge.has("Aisha")); // false

🧩 2️⃣ Iterating Through Maps

for (let [key, value] of userAge) {
  console.log(`${key} is ${value} years old.`);
}

Maps maintain insertion order and are faster for large collections.

πŸ“— 3️⃣ Set — Store Unique Values

A Set stores only unique values. Duplicates are automatically removed.

let fruits = new Set(["apple", "banana", "apple"]);
console.log(fruits); // Set(2) { 'apple', 'banana' }

fruits.add("orange");
fruits.delete("banana");
console.log(fruits.has("orange")); // true

🧠 4️⃣ Convert Between Set & Array

let arr = [1,2,2,3,3,4];
let unique = [...new Set(arr)];
console.log(unique); // [1,2,3,4]

⚙️ 5️⃣ WeakMap — Lightweight Map for Objects

A WeakMap is similar to Map, but it only accepts objects as keys and does not prevent garbage collection.

let weakMap = new WeakMap();
let obj = {};
weakMap.set(obj, "Sensitive Data");

console.log(weakMap.get(obj));
obj = null; // object is removed from memory automatically

🧩 6️⃣ WeakSet — For Object References Only

let weakSet = new WeakSet();
let person = { name: "Rahul" };
weakSet.add(person);
console.log(weakSet.has(person)); // true
person = null; // automatically cleaned up

πŸ’‘ 7️⃣ When to Use What

  • Map → Store key-value pairs with any type of key.
  • Set → Store unique values efficiently.
  • WeakMap → Store temporary object-keyed data.
  • WeakSet → Track object references without memory leaks.

🎯 Summary

Maps and Sets are faster and more memory-efficient than traditional objects and arrays for certain tasks. WeakMaps and WeakSets help manage memory automatically by allowing garbage collection — a key for optimizing apps. Tomorrow you’ll dive into one of the most powerful JavaScript concepts — Closures and Scope.

Comments