Lesson 5.6: Advanced ES6+ Features – Destructuring, Spread/Rest, Optional Chaining
Modern JavaScript (ES6 and beyond) provides advanced features to write cleaner, shorter, and more readable code.
1. Destructuring
-
Allows extracting values from arrays or objects into variables.
Array Destructuring:
Object Destructuring:
2. Spread Operator (...)
-
Expands arrays or objects into individual elements.
-
Useful for copying, merging, or passing arguments.
3. Rest Operator (...)
-
Collects remaining elements or properties into a new array or object.
4. Optional Chaining (?.)
-
Safely access nested object properties without errors if a property doesn’t exist.
5. Practical Uses
-
Write clean and concise code.
-
Avoid runtime errors with optional chaining.
-
Merge or copy arrays and objects easily.
-
Handle function arguments dynamically with rest parameters.
✅ Summary:
-
Destructuring: Extract values from arrays/objects easily.
-
Spread & Rest: Expand or collect elements for flexibility.
-
Optional Chaining: Access nested properties safely.
-
Essential for modern JavaScript development and cleaner code.
