Spread Operator
The JavaScript Spread Operator (three dots …) is used to expand elements from arrays, objects, or function arguments and spread them into a new context or structure.
Syntax
let varName = [ ...value ];
Example 1: In this example, the Spread Operator combines arrays arr1 and arr2 effectively concatenating their elements into the resulting array.
Javascript
// Concatinating two arrays // using Spread Operator let arr1 = [ 4, 5 ]; let arr2 = [ 8, 9, 10 ] arr = [ ...arr1, ...arr2 ]; console.log(arr); |
[ 4, 5, 8, 9, 10 ]
Example 2: In this example, the Spread Operator is used to clone an object (originalObject) and add/modify properties to create a new object. Here, we create a new object copiedObject by cloning obj1 and adding the city property.
Javascript
const obj1 = { name: "Amit" , age: 22 }; const newObject = { ...obj1, city: "Uttarakhand" }; console.log(newObject); |
{ name: 'Amit', age: 22, city: 'Uttarakhand' }
JavaScript Ellipsis
JavaScript Ellipsis (also known as the spread/rest operator) is represented by three dots (…). It is used for various tasks, such as spreading elements of an array into individual values or collecting multiple values into an array or object. It simplifies data manipulation and function parameter handling.
We will explore the basic implementation of the Spread/Rest operator with the help of examples.
Contact Us