ES6+ Modern Features
Master modern JavaScript! Learn template literals, optional chaining, nullish coalescing, and more ES6+ features.
Master modern JavaScript! Learn template literals, optional chaining, nullish coalescing, and more ES6+ features. This interview-focused guide covers essential es6+ modern features concepts for technical interviews.
ES6+ Modern Features
Modern JavaScript (ES6 and later versions) introduced powerful features that make code cleaner, more readable, and more expressive. In this chapter, we'll explore these essential tools.
1. Template Literals π
What are they?
Template literals are a new way to create strings using backticks (`) instead of single or double quotes.
Why use them? They allow for:
- String Interpolation: Embedding variables directly into strings.
- Multi-line Strings: Creating strings that span multiple lines without special characters.
How it works:
Use ${variable} to insert values.
2. Optional Chaining (?.) π
What is it? Optional chaining allows you to safely access nested properties of an object without worrying if an intermediate property exists.
Why use it?
It prevents the dreaded TypeError: Cannot read properties of undefined error. If a property doesn't exist, it simply returns undefined instead of crashing your app.
How it works:
Place ?. before the property you want to access.
3. Nullish Coalescing (??) π―
What is it?
A logical operator that returns the right-hand side operand only when the left-hand side is null or undefined.
Why use it?
The standard OR operator (||) falls back for any falsy value (like 0, "", or false). Sometimes you want to keep 0 or "" as valid values and only fall back if the value is truly missing (null or undefined).
How it works:
left ?? right
4. Default Parameters π
What are they?
Default parameters allow you to initialize named parameters with default values if no value or undefined is passed.
Why use them? To make your functions more robust and handle missing arguments gracefully without writing extra check logic inside the function.
How it works:
Assign a value in the function definition: function(a = 1).
5. Enhanced Object Literals π¨
What are they? Shorthand syntax for defining properties and methods in objects.
Why use them? They reduce boilerplate code when creating objects, making your code cleaner.
How it works:
- Property Shorthand:
{ name }instead of{ name: name } - Method Shorthand:
sayHi() {}instead ofsayHi: function() {}
6. Modern Array & Object Methods π οΈ
ES6+ also added useful utility methods.
Object.entries(): Returns an array of[key, value]pairs.Object.values(): Returns an array of values.Array.from(): Creates an array from an array-like object (like a String or NodeList).
Practical Examples π‘
Let's combine these features into a real-world scenario.
AI Mentor
Confused about "ES6+ features, template literals, optional chaining, nullish coalescing, default parameters, and object enhancements"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 4What is the main benefit of Optional Chaining (?.)?
Key Takeaways
β
Template Literals ( ): Easy string interpolation and multi-line strings.
β
Optional Chaining (?.): Safe access to nested properties.
β
Nullish Coalescing (??): Smarter default values (handles 0 and "" correctly).
β
Default Parameters: Set default values for function arguments.
β
Object Shorthands: Cleaner syntax for creating objects.
What's Next?
These features are used everywhere in modern frameworks like React. Next, let's look at Modules to see how to organize your code!
Keep coding! π