- ✓The difference between const, let, and var, and why immutability matters
- ✓Arrow functions and lexical this binding
- ✓Modern array methods: map, filter, and reduce
- ✓Asynchronous JavaScript: Promises, async/await, and Fetch API
- ✓Interacting with DOM elements and handling user events
1. Variables & Scope in Modern ES6+
In modern JavaScript, forget about `var`. It has function scope and hoisting quirks that introduce subtle bugs. Instead, always use `const` by default for values that won't be reassigned, and `let` only when you explicitly need a variable whose value will mutate.
Using `const` makes your code self-documenting: other developers (and your future self) immediately know that the reference remains constant.
// Declare constants by default
const siteTitle = "Design The Thought";
const categories = ["Web Dev", "Careers", "Projects"];
// Let only for mutable counters or state
let currentStep = 1;
currentStep += 1;
// Array transformations with map and filter
const formattedCategories = categories.map(cat => cat.toUpperCase());
console.log(formattedCategories); // ["WEB DEV", "CAREERS", "PROJECTS"]2. Asynchronous JavaScript: Async / Await and Fetch API
JavaScript is single-threaded, meaning it can only execute one operation at a time. To prevent long network requests from freezing the entire user interface, the browser uses an asynchronous event loop.
The modern standard for fetching network data is `async/await` syntax paired with the native `fetch()` API. It reads like synchronous code while handling network promises non-blockingly.
async function fetchProductData(endpoint) {
try {
const response = await fetch(endpoint);
if (!response.ok) {
throw new Error(`Network error: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error("Failed to load products:", error.message);
return null;
}
}