JavaScript · Lesson 12 of 20 · 3 min
ES6+ Features (Part 1)
This isn't about new syntax; it's about showing you write predictable, modern JavaScript.
After this lesson, you'll be able to explain the exact trade-offs of modern JS features and avoid the common traps candidates fall into. These topics are table stakes in any frontend interview.
Use const and let, forget var
In modern code, there is no reason to use var. Use const by default. If you need to reassign a variable, use let.
The key difference is scope. let and const are block-scoped, meaning they only exist within the nearest set of curly braces ({...}). var is function-scoped, which can cause bugs where variables "leak" out of loops or if blocks.
let and const are also not accessible before they are declared. This is the "Temporal Dead Zone" (TDZ). It prevents you from accidentally using a variable that hasn't been initialized, unlike var which is hoisted and initialized as undefined.
The trap: Believing
constmakes objects or arrays immutable. It does not.constonly prevents reassignment of the variable itself. You can still change the properties of an object or the elements of an array declared withconst.const user = { name: "Alice" }; // This is an error: // user = { name: "Bob" }; // This is perfectly fine: user.name = "Bob";Answering the "difference between
var,let, andconst" is a basic screening question. A crisp answer on scope and the TDZ shows you're current. Mentioning theconstobject mutation trap shows deeper understanding.
Master lexical this with arrow functions
Arrow functions (=>) are not just for shorter syntax. Their primary purpose is to handle the this keyword lexically.
A regular function gets its this value from how it's called (the "call site"). An arrow function inherits this from its parent scope at the time it is defined. This solves a whole class of bugs, especially with callbacks in methods.
class DataFetcher {
constructor(id) {
this.id = id;
this.data = null;
}
// Using a traditional function callback
fetchDataWithBug() {
setTimeout(function() {
// `this` here is NOT the DataFetcher instance.
// In non-strict mode, it's `window`. In strict mode, it's `undefined`.
// This will throw an error.
console.log(`Failed to fetch for ID: ${this.id}`);
}, 100);
}
// Using an arrow function callback
fetchDataCorrectly() {
setTimeout(() => {
// `this` is lexically inherited from the fetchDataCorrectly scope.
// It correctly refers to the DataFetcher instance.
this.data = { content: `Data for ${this.id}` };
console.log(`Fetched: ${this.data.content}`);
}, 100);
}
}
const fetcher = new DataFetcher(42);
// fetcher.fetchDataWithBug(); // Throws TypeError: Cannot read properties of undefined (reading 'id')
fetcher.fetchDataCorrectly(); // Fetched: Data for 42
This is the most common interview scenario for arrow functions. Be ready to explain exactly why the first function fails and the second one works.
The trap: Don't use arrow functions for object methods that need a dynamic this, like event listeners that rely on this referring to the element.
Destructure objects for cleaner APIs
Destructuring is a concise way to extract values from objects and arrays. Its most powerful use is in defining cleaner function signatures and consuming API responses.
It allows you to simulate named parameters and set default values directly in the function signature. This makes the function's contract explicit and easier to use.
// Before: unclear what the boolean and string mean without looking inside.
// createUser("Alice", true, "admin", undefined);
// After: clear, self-documenting, and order doesn't matter.
function createUser({ name, email, role = "Guest", lastLogin = new Date() }) {
console.log(`Creating user ${name} (${email}) with role ${role}.`);
// ...
}
const userData = {
email: "alice@example.com",
name: "Alice",
role: "Admin",
};
createUser(userData);
// "Creating user Alice (alice@example.com) with role Admin."
const basicUser = {
name: "Bob",
email: "bob@example.com",
};
createUser(basicUser);
// "Creating user Bob (bob@example.com) with role Guest."
When an interviewer asks you to process a JSON object, use destructuring. It shows you know how to handle structured data efficiently. For example, to pull user.address.city, you can use const { address: { city } } = response.data;.
Use template literals for readable strings
Template literals (using backticks `) are the standard for creating strings in modern JavaScript. They are cleaner and less error-prone than concatenation with +.
They solve two common problems: embedding expressions and creating multi-line strings.
const user = { name: "Sarah", items: 3 };
const price = 49.99;
// Old way: clumsy, hard to read, easy to miss spaces.
const summaryOld =
"Hello, " +
user.name +
"! You have " +
user.items +
" items in your cart. Your total is $" +
(user.items * price).toFixed(2) +
".";
// New way: clean, readable, expressions embedded directly.
const summaryNew = `Hello, ${user.name}! You have ${
user.items
} items in your cart. Your total is $${(user.items * price).toFixed(2)}.`;
// Also great for multi-line strings without `\n`.
const html = `
<div>
<h2>Welcome, ${user.name}</h2>
</div>
`;
Using template literals is a small signal, but it contributes to an overall impression of clean, modern coding habits.
Try it
Take a small function you've written recently that builds a string or takes a configuration object. Refactor it to use template literals and destructuring for its arguments. Aim to make the function signature self-documenting. This should take about 15 minutes.
Next: We'll cover more ES6+ features in "ES6+ Features (Part 2)", including spread/rest operators, modules, and classes.
Lock it in — apply this lesson under interview conditions.