DEV Community

CatWebDev
CatWebDev

Posted on

Difference Between Spread Syntax (...) and Rest Parameters (...) in JavaScript.

Intro

While the spread syntax (...) and rest parameters (...) in JavaScript look similar, they serve different purposes depending on how and where they are used. Both are used to manipulate arrays and objects, but understanding the difference is essential for using them correctly in your code.

Spread Syntax

The spread syntax is used to expand elements of an array or object. It takes an iterable (like an array or object) and expands it into its elements. We typically use spread syntax in function arguments, array elements, or object properties.

Example: Using Spread with Arrays

const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];

console.log(newNumbers); // Output: [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Example: Using Spread with Objects

const obj = { a: 1, b: 2 };
const newObj = { ...obj, c: 3 };

console.log(newObj); // Output: { a: 1, b: 2, c: 3 }
Enter fullscreen mode Exit fullscreen mode

Purpose of Spread Syntax:

  • Expands an array or object into individual elements or properties.
  • Used in array/object copying, merging, or passing elements as function arguments.

Rest Parameters

The rest parameter collects multiple elements into a single array or object. It allows a function to accept an indefinite number of arguments, capturing them into an array. Unlike spread syntax, the rest parameter gathers values rather than expands them.

Example: Using Rest Parameters in Functions

function sum(...numbers) {
  return numbers.reduce((total, num) => total + num, 0);
}

console.log(sum(1, 2, 3, 4)); // Output: 10
Enter fullscreen mode Exit fullscreen mode

Example: Using Rest in Array Destructuring

const [first, ...rest] = [1, 2, 3, 4];

console.log(first); // Output: 1
console.log(rest);  // Output: [2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

Purpose of Rest Parameters:

  • Gathers multiple arguments into a single array.
  • Useful for functions that can accept any number of arguments or when you want to collect “the rest” of the elements in array/object restructuring.

Key Differences

Functionality:

  • Spread Syntax: Expands an array or object into individual elements or properties.
  • Rest Parameters: Gathers multiple elements into a single array or object.

Usage:

  • Spread Syntax: Used when passing or merging elements.
  • Rest Parameters: Used to collect arguments or remaining elements in restructuring.

Context:

  • Spread Syntax: Typically used in function calls, array copying, and object merging.
  • Rest Parameters: Used primarily in function definitions to collect arguments and in restructuring.

In the end

Though they share the same (…) syntax, the spread syntax, and the rest parameters have distinct uses. Spread is used to expand elements, while rest is used to gather elements. Mastering both will enhance your ability to work with arrays, objects and functions cleanly and efficiently.

Thank you for reading!

Visit my YouTube channel CatWebDev.

Your likes, comments, and subscriptions are greatly appreciated. Thank you for the support!

Top comments (0)