In the previous post of this JavaScript course we have shown that how arrow functions work using code examples. In this part of the course we are covering two topics destructuring and rest operators. First we will address destructuring feature.
JavaScript ES6+ Free Crash Course 2025 – Key ES6+ Features – Arrow Functions
1.2 Destructuring
Destructuring allows you to extract values from arrays or properties from objects into distinct variables.
// Array destructuring
const [first, second, ...rest] = [1, 2, 3, 4, 5];
console.log(first, second, rest); // 1 2 [3, 4, 5]
// Object destructuring
const person = { name: 'John', age: 30, city: 'New York' }; const { name, age } = person;
console.log(name, age); // John 30
// With different variable names
const { name: personName, age: personAge } = person; console.log(personName, personAge); // John 30
// Nested destructuring
const { address: { street } } = { address: { street: 'Main St' } }; console.log(street); // Main St
Best Practices:
- Use destructuring to make function parameters more explicit. Destructure in function parameters for cleaner code
Common Pitfalls:
- Destructuring undefined or null values can cause errors
- Be careful with deeply nested destructuring, as it can make code less readable
1.3 Spread/Rest Operators
The spread operator (…) expands an iterable into individual elements, while the rest operator collects multiple elements into an array.
const obj1 = { x: 1, y: 2 };
const obj2 = { ...obj1, z: 3 };
console.log(obj2); // { x: 1, y: 2, z: 3 }
// Rest operator
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3, 4)); // 10
const [first, ...others] = [1, 2, 3, 4, 5];
console.log(first, others); // 1 [2, 3, 4, 5]
Best Practices
- Use spread to create shallow copies of arrays and objects. Use rest parameters instead of the arguments object.
Common Pitfalls
- Spread only creates a shallow copy, nested objects are still referenced
- Rest parameter must be the last formal parameter in a function












