Explain the benefits of spread syntax & how it is different from rest syntax in ES6 ?
Last Updated :
21 Jul, 2022
Spread Operator: Spread operator or Spread Syntax allow us to expand the arrays and objects into elements in the case of an array and key-value pairs in the case of an object. The spread syntax is represented by three dots (...) in JavaScript.
Syntax:
var my_var = [...array];
Benefits of using Spread syntax:
1. It allows us to include all elements of an array or object in a list of some kind. It can expand an object or an array and store all values in a new variable of the same datatype.
Example:
JavaScript
<script>
// Operating spread syntax to expand an Object
var obj1 = {
organisation: "GFG",
fullForm: "Geeks for Geeks"
};
var obj2 = {
description: "A learning platform for Geeks."
};
var obj3 = {...obj2, ...obj1
};
console.log(obj3);
// Operating spread syntax to expand an Array
var fruits = ["Mango", "Banana", "Apple"];
var moreFruits = [...arr1, "Orange",
"Pineapple", "Watermelon"];
console.log(moreFruits);
</script>
Output:
Expanding an Array and an Object
2. It allows us to copy all elements of an already existing array into a new array and perform push, pop operations without even disturbing the previous array.
Example:
JavaScript
<script>
// Copying an array into another
// array and operate pop
// operation without disturbing
// data of first array
var arr1 = ['a', 'b', 'c', 'd', 'e'];
var arr2 = [...arr1];
console.log("arr1 before applying pop operation:");
console.log(arr1);
arr2.pop();
console.log("arr1 & arr2 after applying"+
" pop operation on arr2:");
console.log(arr2);
console.log(arr1);
</script>
Output:

3. It can concat a string or merge two or more arrays without using concat() function.
Example:
JavaScript
<script>
// concatenation of two arrays without
// concat function using spread syntax
var breakfast = ["Bread", "Sandwich", "Fruits"];
var moreBreakfast = ["Salad", "Tea & Coffee"];
console.log("Breakfast before concatenation:");
console.log(breakfast);
breakfast = [...breakfast, ...moreBreakfast];
console.log("Breakfast after concatenation:");
console.log(breakfast);
</script>
Output:

4. It allows us to operate Math functions on an array. Because we can't use Math functions directly on arrays. The spread operator expands an iterable object array into a list of arguments that allow us to operate the Math functions and get the desired result.
Example:
JavaScript
<script>
// Operating an array in an Math
// function using spread operator
var arr1 = [99, 50, 130, 1, 98, 23, 66];
console.log("Maximum element of arr1 is:")
console.log(Math.max(...arr1));
</script>
Output:

5. It allows us to do changes in nested object without reflecting in original object
Example :Â
Structure of pointing object in nested object
Â
JavaScript
let obj = {
name:'Geeks for Geeks',
add:{
country:"INDIA",
state:{
code:"DL",
pincode:"129089"
}
}
}
let obj2 = {...obj} ///SHALLOW COPY
obj2.name = "GFG",
console.log(obj2);
obj2 = {...obj2,add:{...obj.add}}
obj2.add.country = "BHARAT"
console.log(obj2)
obj2 = {...obj2,add:{...obj2.add,state:{...obj.add.state}}} //DEEP COPY
obj2.add.state.pincode = 823687
console.log("original object")
console.log(obj);
console.log("doing all changes final object")
console.log(obj2);
Â
Rest operator: The syntax of the Rest operator is similar to that of Spread operator syntax, where the spread operator expands an array into its elements on the other hand rest syntax is used to collect data and store that data into a single variable which we can further pass to a function or can use in our code as a variable.
Syntax:
function nameOfFunction(...data)
{
// function statement
}
Example 1: The below example illustrates the use of rest syntax.
JavaScript
<script>
var sum = 0;
function Addition(...data) {
for(var i = 0; i < data.length; i++) {
sum += data[i];
}
console.log("Sum of your passed data is:")
console.log(sum);
}
Addition(95, 98, 2, 51, 6);
</script>
Output:
Getting the sum of passed data using rest syntax
Example 2:
JavaScript
<script>
var pro = 1;
function Multiplication(...param) {
for(var item = 0; item < param.length; item++) {
pro *= param[item];
}
if(pro < 100) {
console.log("Product is less than 100:");
console.log("Product: ", pro);
pro = 1;
console.log("Your new product is: ");
Multiplication(1, 2, 3, 4, 5);
console.log("Product: ", pro);
}
}
Multiplication(1, 2, 3, 4);
</script>
Output:
Getting the product of passed data using rest syntax
Differences between Rest and Spread Syntax are enclosed in the table below:
                 Spread Syntax |                                Rest Syntax |
Spread operator as its name suggests it spreads or expands the content of the given element. | Rest Syntax is just the opposite of spread syntax it collects the data and stores that data in a variable which we can use further in our code. |
It expands an Array in form of elements, while in key-value pairs in the case of Objects. | It collects the data in the developer's desired format. |
You may or may not use the strict mode inside the function containing the spread operator. | You can not use the strict mode inside function containing the rest operator. |
It will overwrite the identical properties inside two objects and replace the former with the latter. | It simply collects all properties and wraps them inside a container. |
Similar Reads
How ReactJS ES6 syntax is different compared to ES5 ?
Both ES6 and ES5 are Javascript scripting languages in the development industry. ECMA Script or ES is a trademarked scripting language made by ECMA International. The European Computer Manufacture Association or ECMA is used for client-side scripting for the worldwide web. ES5 was released in 2009 a
6 min read
Difference between res.send() and res.json() in Express.js?
In this article, we will learn about res.send() & res.json(), along with discussing the significant distinction that differentiates between res.send() & res.json(). Let us first understand what is res.send() and res.json() in Express.js? res.send() - The res.send() function is used for sendi
4 min read
Explain the Benefits of State Normalization in Redux
Redux is a predictable state container for JavaScript apps and offers a powerful solution for managing application states in complex web applications. One strategy that Redux uses is state normalization in which we can restructure state data to improve performance, maintainability, and scalability.
5 min read
Explain the differences on the usage of foo between function foo() {} and var foo = function() {}
Here we see the differences on the usage of foo between function foo() {} and var foo = function() {} types of function declarations in JavaScript. function foo() {} is a Normal Function or traditional general way of Function Declaration which every user or developer finds it simpler to declare and
4 min read
What is the difference between export.create and router.post?
export.create and router.post are used in the context of backend frameworks like Express JS. They both are used for handling HTTP requests and operate at different levels within the framework. In this article we will learn about export.create and router.post and see the key differences between them.
3 min read
Explain the use of req and res objects in Express JS
Express JS is used to build RESTful APIs with Node.js. We have a 'req' (request) object in Express JS which is used to represent the incoming HTTP request that consists of data like parameters, query strings, and also the request body. Along with this, we have 'res' (response) which is used to send
4 min read
What is the difference between React Components in ES5 and ES6 ?
In this article, we will learn about difference between ES5 and ES6 React Components by comparing their individual components and features Table of Content ES5 ComponentsES5 FeaturesES6 ComponentsES6 FeaturesDifference between React Components in ES5 and ES6 ES5 ComponentsES5 (ECMAScript 5) is a sta
5 min read
How to copy properties from one object to another in ES6 ?
The properties of one object can be copied into another object through different methods. Here are some of those methods. Object.assign(): By using the Object.assign() method, all enumerable properties of one or more source objects are copied to the target object. This method returns the modified ta
3 min read
Difference between PUT and POST HTTP Request in Express and Postman
Both PUT and POST are request methods used in the HTTP protocol. In this article, we will introduce the HTTP methods such as PUT and POST in detail, and we will also discuss in detail when to use PUT and when to use POST. Table of Content PUT methodPOST MethodDifference between PUT and POST methodCo
5 min read
What is the difference between every() and some() methods in JavaScript ?
In this article, we will see the difference between every() and some() methods in JavaScript. Array.every() methodThe Array.every() method in JavaScript is used to check whether all the elements of the array satisfy the given condition or not. The output will be false if even one value does not sati
3 min read