JavaScript · Lesson 13 of 20 · 4 min
ES6+ Features (Part 2)
Justify your code structure and data structure choices with confidence.
After this, you'll know when to reach for Classes, Modules, Maps, and Sets. More importantly, you'll be able to explain why you chose them over older alternatives, which is a key signal of seniority in an interview.
When to use class (and when not to)
The class keyword is syntactic sugar over JavaScript's prototype-based inheritance. You must be able to state this. It doesn’t introduce a new object model; it just provides a cleaner, more familiar syntax for creating object blueprints.
Its main value is grouping related data (properties) and behavior (methods). Use extends to inherit from a parent class. Always call super() in the child constructor before using this to properly set up the inheritance chain.
The trap: Over-relying on deep inheritance chains (A extends B extends C). This often leads to rigid, hard-to-refactor code. An interviewer might ask you to refactor an inheritance hierarchy into a more flexible pattern using composition. Be prepared to discuss the trade-offs.
A static method belongs to the class itself, not to an instance. Think of them as utility functions namespaced by the class. Date.now() is a native example; it makes no sense to call new Date().now().
This diagram shows the constructor flow when a derived class is instantiated. The call to super() is crucial.
How modules enforce code boundaries
ES Modules (import/export) are the standard for organizing any non-trivial JavaScript application. They let you break code into small, focused files with explicit dependencies.
Each module has its own scope. Variables declared in one file are not available in another unless you export them. This prevents global scope pollution and makes your code's dependency graph explicit and easier to reason about.
An interviewer asking "how would you structure this feature?" is looking for an answer that starts with breaking it down into modules.
Named vs. Default Exports: A Point of View A module can have many named exports but only one
default.
export const a = 1;import { a } from './utils';
export default function main() { /* ... */ }import myMainFunction from './utils';The trap: Overusing
export default. It allows the importer to name the import anything they want, leading to inconsistency across the codebase. Prefer named exports for clarity and easier static analysis. Use a default export only for the single, primary export of a file, like a React component.
Build tools use the static structure of import and export to perform tree-shaking, which removes unused code from your final bundle. This is a performance win you get by using modules correctly.
Choosing Map or Set over Objects and Arrays
This is a classic "choose the right data structure" question. Your ability to explain the trade-offs is what's being tested.
Use Map when you need keys that are not strings. This is the primary reason to use Map over a plain {} object. You can use any value (an object, a function) as a key, which is impossible with plain objects that coerce all keys to strings. This is useful for associating metadata with objects without modifying them.
Map also guarantees insertion order when iterating and provides a .size property for an O(1) count of its entries. Getting the size of a plain object requires Object.keys(obj).length, which is O(n).
Use Set for collections of unique values. The most common interview use case is de-duplicating an array: const unique = [...new Set(myArray)];. You should be able to write this from memory.
Set provides a highly efficient .has() method. For a Set, .has(value) is O(1) on average. For an Array, .includes(value) is O(n). This difference is significant for large datasets.
// A base class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `Hello, my name is ${this.name}.`;
}
}
// A derived class
class Developer extends Person {
constructor(name, age, language) {
super(name, age); // Call the parent constructor
this.language = language;
}
// Method override
greet() {
return `${super.greet()} I write ${this.language}.`;
}
}
// Using Map to associate objects with metadata
const projectAssignments = new Map();
const alice = new Developer('Alice', 30, 'JavaScript');
const bob = new Developer('Bob', 25, 'Python');
projectAssignments.set(alice, 'Project Phoenix');
projectAssignments.set(bob, 'Project Chimera');
console.log(`${alice.name}'s assignment: ${projectAssignments.get(alice)}`);
// Output: Alice's assignment: Project Phoenix
// Using Set to find unique languages
const languages = new Set();
languages.add(alice.language);
languages.add(bob.language);
languages.add('JavaScript'); // This duplicate is ignored
console.log(`Team languages: ${Array.from(languages).join(', ')}`);
// Output: Team languages: JavaScript, Python
Try it
Build a simple Cache class. It should have get(key), set(key, value), and has(key) methods. Internally, it must use a Map to store the data. Then, add a feature: the set method should also take an optional third argument, ttl (time-to-live in milliseconds). If a ttl is provided, the entry should automatically be removed from the cache after that duration. This will take you about 20 minutes.
Next: We move on to handling runtime issues in the next lesson, "Error Handling & Debugging".
Lock it in — apply this lesson under interview conditions.