Syntax of spread operator:
...spreadSyntax of rest operator:
...restIn JavaScript, both spread and rest operators use the same syntax, but they do different things depending on the context.
Spread operator expands (spreads out) elements, while rest operator collects (gathers) elements into a single variable.
Let’s understand both with simple examples.
Spread Operator
The spread operator (...) allows you to expand elements of an array or object.
Spread in Arrays
const colors = ["red", "green"];
const moreColors = [...colors, "blue", "yellow"];
console.log(moreColors); // Output: ["red", "green", "blue", "yellow"]Here,
colorsis an array....colorsuses the spread operator to unpack the elements inside thecolorsarray.- That means it will take everything from the
colorsarray and pull the values out one by one. Here you’re not copying the array, you’re spreading its items out into a new array. - So
...colorsbecomes, "red", "green" and then you're adding "blue" and "yellow" after that. - You created a new array without affecting the original one.
Copying Arrays
const original = [1, 2, 3];
const copy = [...original];
copy.push(4);
console.log(original); // Output: [1, 2, 3]
console.log(copy); // Output: [1, 2, 3, 4]Here,
originalis an array.copy = [...original]uses the spread operator to create a new array with the same values.- That means it'll take out the values from
originaland make a new array with them. - You created a new array without affecting the original one.
- Then
copy.push(4)adds 4 to the new arraycopy, not theoriginal.
Merge Arrays
const a = [1, 2];
const b = [3, 4];
const merged = [...a, ...b];
console.log(merged); // Output: [1, 2, 3, 4]Here,
...aspreads the values: 1, 2...bspreads the values: 3, 4- So,
const merged = [...a, ...b];becomesconst merged = [1, 2, 3, 4];
Spread in Objects
const admin = { name: "Shefali" };
const profile = { ...admin, age: 26 };
console.log(profile); // Output: { name: "Shefali", age: 26 }Here,
adminis an object....adminuses the spread operator to copy all properties fromadmininto a new object.- Then
age: 26is added to that new object. - So the new object
profilebecomes{ name: "Shefali", age: 26 }.
If two properties have the same name, the one written later will replace the earlier one.
For example:
const admin = { name: "Shefali" };
const update = { name: "Learnify" };
const result = { ...admin, ...update };
console.log(result); // Output: { name: "Learnify" }Rest Operator
The rest operator (...) allows you to collect multiple values into a single variable.
Rest in Array Destructuring
const colors = ["red", "green", "blue", "yellow"];
const [first, ...others] = colors;
console.log(first); // Output: "red"
console.log(others); // Output: ["green", "blue", "yellow"]Here,
- The array
colorshas four items. [first, ...others] = colors;is destructuring the array:firstgets the first item"red"....othersuses the rest operator to gather all remaining items into a new array.
- So
firstis"red"andothersis["green", "blue", "yellow"].
Rest in Object Destructuring
const admin = { name: "Shefali", age: 26, country: "India" };
const { name, ...rest } = admin;
console.log(name); // Output: "Shefali"
console.log(rest); // Output: { age: 26, country: "India" }Here,
- The object
adminhas three properties. { name, ...rest } = admin;destructures the object:namegets the value"Shefali"....restuses the rest operator to collect all other properties into a new object.
- So
nameis"Shefali"andrestis{ age: 26, country: "India" }.
Rest in Function Parameters
You can use rest operator to accept any number of arguments.
For example:
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3, 4)); // Output: 10Here,
- The
...numbersin the function parameter uses the rest operator to collect all arguments into an array callednumbers. - Inside the function,
numbers.reduce((a, b) => a + b, 0)adds up all the numbers in the array. - So when you call
sum(1, 2, 3, 4), it adds all those numbers and returns10.
Tip: The spread operator is often used to copy or merge arrays/objects, while the rest operator is used to collect remaining elements or skip some values.
👉 Next tutorial: JavaScript Map and Set