What is the Spread Operator?
The spread operator (...) allows an iterable (array, string, etc.) to be expanded in places where zero or more elements are expected. It's the opposite of rest parameters.
Spread operators are useful for copying arrays, merging arrays, passing arguments, and spreading object properties.
Basic Spread Operator
// Spreading an array into function arguments
const numbers = [1, 2, 3];
console.log(Math.max(...numbers)); // Same as Math.max(1, 2, 3)
// Output: 3
// Copying arrays
const arr1 = [1, 2, 3];
const arr2 = [...arr1]; // Creates a new array
console.log(arr2); // [1, 2, 3]
// Merging arrays
const arr3 = [1, 2];
const arr4 = [3, 4];
const merged = [...arr3, ...arr4];
console.log(merged); // [1, 2, 3, 4]
// Spreading strings
const str = "hello";
const chars = [...str];
console.log(chars); // ['h', 'e', 'l', 'l', 'o']Spread operator expands iterables into individual elements.