Member-only story

Mastering Rest Parameters and Spread Syntax in ES6 Destructuring

A Practical Guide to Simplifying Your JavaScript Code

Max N
3 min readApr 2, 2024

In the ever-evolving world of JavaScript, ES6 (ECMAScript 2015) introduced several powerful features that have revolutionized the way we write and structure our code. Among these features are rest parameters and spread syntax, which have become invaluable tools for working with arrays and objects in a more concise and expressive manner.

In this article, we’ll explore these concepts and provide practical examples to help you level up your JavaScript skills.

Rest Parameters: Gathering the Remaining Elements

Rest parameters allow you to collect the remaining elements of an array or object into a new array or object. This feature is particularly useful when you need to work with a variable number of arguments or when you want to extract specific elements from an array or object while keeping the rest intact.

// Gathering remaining elements in an array
function sum(a, b, ...rest) {
console.log(a, b); // Output: 1 2
console.log(rest); // Output: [3, 4, 5]
return a + b + rest.reduce((acc, curr) => acc + curr, 0);
}

console.log(sum(1, 2, 3, 4, 5)); // Output: 15

// Gathering remaining properties in an object
const…

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet