Stage 5: Jumping into JavaScript
Rest and Spread Operators (...)

In JavaScript, the rest and spread operators are two powerful features that allow you to work with arrays and objects in a flexible and concise way. The rest operator, denoted by ..., allows you to represent an indefinite number of arguments as an array. For example:

function sum(...numbers) {
  let total = 0;
  for (let number of numbers) {
    total += number;
  }
  return total;
}
 
console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(4, 5, 6, 7)); // Output: 22

In this example, the sum() function takes an indefinite number of arguments and uses the rest operator to store them in an array called numbers. The function then uses a loop to add up the numbers in the array and return the total. The spread operator, also denoted by ..., allows you to spread the elements of an array or object into another array or object. For example:

const numbers1 = [1, 2, 3];
const numbers2 = [4, 5, 6];
const combinedNumbers = [...numbers1, ...numbers2];
console.log(combinedNumbers); // Output: [1, 2, 3, 4, 5, 6]

In this example, the spread operator is used to combine the elements of two arrays, numbers1 and numbers2, into a new array called combinedNumbers. The spread operator can also be used to create a new object by combining the properties of two or more objects:

const person1 = { name: 'John', age: 30 };
const person2 = { address: '123 Main St', phone: '555-555-5555' };
const combinedPerson = { ...person1, ...person2 };
 
console.log(combinedPerson); // Output: { name: "John", age: 30, address: "123 Main St", phone: "555-555-5555" }

In this example, the spread operator is used to combine the properties of two objects, person1 and person2, into a new object called combinedPerson. In summary, the rest and spread operators are two powerful features in JavaScript that allow you to work with arrays and objects in a flexible and concise way. The rest operator allows you to represent an indefinite number of arguments as an array, while the spread operator allows you to spread the elements of an array or object into another array or object.