Topic 09 / 15
ES6+ Essentials You’ll Use Everywhere
The Patterns You’ll See in Every Codebase
Earlier topics introduced template literals, destructuring, spread, arrows, ?. and ??. This topic rounds out the modern toolkit.
Short-Circuit Patterns
// guard execution
isLoggedIn && showDashboard();
// conditional property (spread tricks)
const payload = {
name,
...(coupon && { coupon }), // include coupon only if truthy
};Map — When Object Keys Aren’t Strings
const cache = new Map();
cache.set("/api/users", usersData);
cache.set(someObject, "metadata"); // ANY type as key
cache.get("/api/users");
cache.has("/api/users"); // true
cache.size;
cache.delete("/api/users");
for (const [key, value] of cache) { ... }Use Map for dynamic key-value data (caches, lookups); plain objects for fixed-shape records.
Set — Uniqueness & Fast Membership
const seen = new Set();
seen.add("ravi");
seen.add("ravi"); // ignored
seen.has("ravi"); // true — O(1)
const unique = [...new Set(emails)]; // classic dedupeJSON
const json = JSON.stringify({ name: "Ravi", score: 82 });
// '{"name":"Ravi","score":82}'
JSON.stringify(data, null, 2); // pretty-printed
const obj = JSON.parse(json);
// deep copy idiom (modern version):
const copy = structuredClone(obj);Useful String Methods
" hi ".trim()
"abc".includes("b") // true
"file.pdf".endsWith(".pdf") // true
"ha".repeat(3) // 'hahaha'
"5".padStart(2, "0") // '05'
"A-B-C".split("-") // ['A','B','C']
"hello".replaceAll("l", "L") // 'heLLo'Dates
const now = new Date();
now.getFullYear(); // 2026
now.toISOString(); // '2026-06-10T09:30:00.000Z'
Date.now(); // ms timestamp
// human formatting — use Intl, not string-building
new Intl.DateTimeFormat("en-IN", { dateStyle: "medium" }).format(now);
// '10 Jun 2026'
new Intl.NumberFormat("en-IN", { style: "currency", currency: "INR" }).format(199);
// '₹199.00'Numbers & Math
Math.round(4.6) // 5
Math.floor(4.6) // 4
Math.max(...arr)
Math.random() // 0 ≤ x < 1
(1234.5678).toFixed(2) // '1234.57' (a string!)