Spread Operator in JavaScript
Spread Operator (...)
Expand arrays/objects/iterables inline.
Spread Examples
// Array spread
let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]
// Function arguments
Math.max(...arr1); // 3
// Object spread
let obj1 = {a: 1, b: 2};
let obj2 = {...obj1, c: 3}; // {a:1, b:2, c:3}
// Clone arrays/objects
let cloned = [...originalArray];
let clonedObj = {...originalObj};Key Points
- Shallow copy for arrays/objects.
- Array concatenation without concat().
- Function call with dynamic arguments.
- Merges objects (later overrides earlier).