The rest operator lets us call any function with any number of arguments and then access the excess arguments as an array. You can also use the rest operator to destroy objects or arrays.

Spread operator (...) lets you expand an iterable-like array into its individual elements.

Spread operator vs. Rest operator
Both the spread operator and rest parameters have the same syntax, which is three dots. Despite having the same syntax, they differ in their functions.

Examples of Spread Operator

1. The following example shows two arrays that are separated by the spread operator (...). They're combined into one using this method. The elements of the merged array are in the same order as they were merged.

var array1 = [10, 20, 30, 40, 50];
var array2 = [60, 70, 80, 90, 100];
var array3 = [...array1, ...array2];
console.log(array3);

Output

[ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ]

2. This example shows how to add an element to an iterable. A given array must be defined. We use the spread operator, which spreads all the values of iterables and adds the elements to the array in the order we choose.

const array1 = [10, 20, 30, 40, 50, 60];
const array2 = [...array1, 5, 55];
console.log(array2);

Output

[ 10, 20, 30, 40, 50, 60, 5, 55 ]

3. This example will show how to copy objects with the spread operator. Spread operator (...). gives obj2 all the properties of the original obj1. Curly brackets are required to indicate that an object is being copied, or else an error will be raised.

const array1 = [10, 20, 30, 40, 50, 60];
const array2 = [ ...array1 ];
console.log(array2);

Output

[ 10, 20, 30, 40, 50, 60 ]

BY Best Interview Question ON 24 Aug 2022