Spread Operator is a very simple and powerful feature introduced in the ES6 standard of JavaScript, which helps us to write nicer and shorter code. The JavaScript spread operator is denoted by three dots (...). The spread operator helps the iterable objects to expand into individual elements. Iterable objects are those on which we can use a loop, for example, Array, Map, Set, etc. In other words, the spread operator allows us to copy all elements from the existing array or object into another array or object.
Let us now visit the following section which will help us to understand the need for the Spread Operator in ES6:
Why there is a need for a spread operator?
An object creates a memory in a heap because the value is defined in a heap so if we copy the original object into the temporary object and do some changes then it will also reflect in the original object the same reason for the array as Array is also behaving as the object.
But the spread operator does not make changes in the original array it also does operations in the spread operator.
Syntax:
var variablename1 = [...value];
Let's understand the usage of the spread operator through the following illustrated examples:
Example 1: Here we have copied the array (from cars1 to cars2 and cars1 to cars3 ) by using the spread operator.
JavaScript
const cars1 = ["AUDI", "BMW", "TATA", "MERCEDES"];
// Copied elements from cars1 to cars2 here
const cars2 = [...cars1];
// Copied elements from cars1 to cars3 here
// and also add some new elements in cars3
const cars3 = [...cars1, "NISSAN", "TOYOTA"];
console.log(cars1);
console.log(cars2);
console.log(cars3);
Output:
[ 'AUDI', 'BMW', 'TATA', 'MERCEDES' ]
[ 'AUDI', 'BMW', 'TATA', 'MERCEDES' ]
[ 'AUDI', 'BMW', 'TATA', 'MERCEDES', 'NISSAN', 'TOYOTA' ]
Example 2: In this example, we will use the spread operator for merging arrays.
JavaScript
const cars1 = ["AUDI", "BMW", "TATA", "MERCEDES"];
const cars2 = ["NISSAN", "TOYOTA"];
// copied elements from cars1 and cars2 to cars3 here
const cars3 = [...cars1, ...cars2];
console.log(cars3);
Output:
[ 'AUDI', 'BMW', 'TATA', 'MERCEDES', 'NISSAN', 'TOYOTA' ]
Example 3: In this example, we will use the spread operator with objects.
JavaScript
// cars1 object
const cars1 = {
Brand: "BMW",
Color: "RED"
}
// copy cars1 object using spread
// operator to create cars2 object
const cars2 = { ...cars1 };
console.log(cars2);
Output:
{ Brand: 'BMW', Color: 'RED' }
Example 4: In this example, we will use the spread operator for merging objects.
JavaScript
// cars1 is a object which is containing
// the attributes Brand & Color
const cars1 = {
Brand: "Toyota",
Color: "RED"
}
// cars2 is a object which is containing
// the attributes Brand, Color & Year
const cars2 = {
Brand: "Nissan",
Color: "BLUE",
Year: 2004
}
const cars3 = { ...cars1, ...cars2 };
console.log(cars3);
Output:
{ Brand: 'Nissan', Color: 'BLUE', Year: 2004 }
Example 5: In this example, We will use the spread operator to split the string into an array of strings.
JavaScript
const car = "AUDI";
const a = [...car];
console.log(a);
Output:
[ 'A', 'U', 'D', 'I' ]
Example 6: Here we will use the spread operator with Array Destructuring.
JavaScript
const numbers = [3, 5, 7, 8, 9];
// Here we assign a,b and c with 3,5 and 7,
// the rest of the elements will all go to others
const [a, b, c, ...others] = numbers;
console.log(a);
console.log(b);
console.log(c);
console.log(others);
Output:
3
5
7
[ 8, 9 ]
Similar Reads
ES6 Operators An expression is a special kind of statement that evaluates to a value. Every expression consists of Operands: Represents the data.Operator: which performs certain operations on operands. Consider the following expression - 2 / 3, in the expression, 2 and 3 are operands and the symbol /is the operat
7 min read
JavaScript Spread Operator The Spread operator (represented as three dots or â¦) is used on iterables like array and string, or properties of Objects, to expand wherever zero or more elements are required to be copied or assigned.1. Adding Multiple Elements Using Spread OperatorEven though we get the content on one array insid
4 min read
How Spread Operator Works in JS The spread operator (...) in JavaScript is a powerful feature used to expand or spread elements of an iterable (like an array or object) into individual elements. It is commonly used in situations where you want to copy or merge arrays, objects, or even pass arguments to functions.1. Arrays with the
3 min read
R-Operators Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. R supports majorly four kinds
5 min read
Spread vs Rest operator in JavaScript ES6 Rest and spread operators may appear similar in notation, but they serve distinct purposes in JavaScript, which can sometimes lead to confusion. Let's delve into their differences and how each is used. Rest and spread operators are both introduced in javascript ES6. Rest OperatorThe rest operator is
2 min read
Lodash _.spread() Method Lodash _.spread() method is used to create a function that calls the given function as a parameter using this binding of the create function, along with an array of arguments. It is based on the spread operator in JavaScript.Syntax:_.spread(func, start);Parameters: func: It is the function that is u
2 min read