Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Spread and Rest Operators in JavaScript


The spread and rest operators, both represented by three dots (...), are powerful tools in JavaScript that make working with arrays, objects, and functions easier. Despite sharing the same syntax, the spread and rest operators serve different purposes. The spread operator is used to expand elements, while the rest operator is used to gather elements into a collection. In this article, we will explore both operators and provide examples of how to use them.

1. Spread Operator

The spread operator (...) allows you to expand an array or object into individual elements. This is useful for combining arrays, creating shallow copies, and passing elements as arguments to functions.

Example 1: Expanding an Array

The spread operator can be used to spread the elements of an array into a new array or as function arguments.

          
          let numbers = [1, 2, 3];
          let newNumbers = [...numbers, 4, 5, 6];
          
          console.log(newNumbers); // Outputs: [1, 2, 3, 4, 5, 6]
          
      

In this example, [...numbers, 4, 5, 6] expands the elements of the numbers array and adds new elements to create a new array.

Example 2: Combining Arrays

The spread operator is useful for merging arrays by expanding the elements of multiple arrays into a new array.

          
          let array1 = [1, 2, 3];
          let array2 = [4, 5, 6];
          let combinedArray = [...array1, ...array2];
          
          console.log(combinedArray); // Outputs: [1, 2, 3, 4, 5, 6]
          
      

This example demonstrates how to combine two arrays using the spread operator.

Example 3: Copying Arrays

The spread operator can also be used to create a shallow copy of an array.

          
          let originalArray = [1, 2, 3];
          let copiedArray = [...originalArray];
          
          console.log(copiedArray); // Outputs: [1, 2, 3]
          
      

Using [...originalArray] creates a new array that copies the elements of originalArray.

Example 4: Using Spread with Function Arguments

The spread operator allows you to pass elements of an array as individual arguments to a function.

          
          function sum(a, b, c) {
              return a + b + c;
          }

          let numbers = [1, 2, 3];
          console.log(sum(...numbers)); // Outputs: 6
          
      

In this example, sum(...numbers) spreads the array elements and passes them as separate arguments to the function.

2. Rest Operator

The rest operator (...) is used to collect multiple elements and condense them into a single array or object. This is especially useful when working with function parameters or destructuring assignments.

Example 1: Using Rest with Function Parameters

The rest operator can be used to gather a variable number of arguments in a function into an array.

          
          function multiply(multiplier, ...numbers) {
              return numbers.map(number => number * multiplier);
          }

          console.log(multiply(2, 1, 2, 3)); // Outputs: [2, 4, 6]
          
      

In this example, the ...numbers parameter gathers the arguments into an array, allowing the function to handle any number of arguments after multiplier.

Example 2: Using Rest in Array Destructuring

The rest operator can be used in destructuring to gather remaining elements of an array into a single variable.

          
          let fruits = ["Apple", "Banana", "Orange", "Mango"];
          let [first, ...restFruits] = fruits;
          
          console.log(first);       // Outputs: Apple
          console.log(restFruits);  // Outputs: ["Banana", "Orange", "Mango"]
          
      

In this example, the rest operator gathers all elements after the first one into the restFruits array.

Example 3: Using Rest in Object Destructuring

The rest operator can also be used with objects to gather remaining properties into a new object.

          
          let person = { name: "Alice", age: 25, city: "New York" };
          let { name, ...otherDetails } = person;
          
          console.log(name);         // Outputs: Alice
          console.log(otherDetails); // Outputs: { age: 25, city: "New York" }
          
      

Here, name is assigned to a separate variable, and the rest operator gathers the remaining properties into the otherDetails object.

3. Combining Spread and Rest Operators

The spread and rest operators can be combined for advanced operations, such as adding items to an array or copying and updating objects.

Example: Adding New Items to an Array

The spread operator can be used to add new items to the beginning or end of an array.

          
          let originalArray = [2, 3, 4];
          let newArray = [1, ...originalArray, 5];
          
          console.log(newArray); // Outputs: [1, 2, 3, 4, 5]
          
      

This example uses the spread operator to add new elements to the start and end of an array.

Example: Updating Properties in an Object

Combining spread and rest can be used to copy an object and update specific properties.

          
          let person = { name: "Alice", age: 25, city: "New York" };
          let updatedPerson = { ...person, age: 26 };
          
          console.log(updatedPerson); // Outputs: { name: "Alice", age: 26, city: "New York" }
          
      

In this example, the spread operator copies the properties of person into a new object and then updates the age property to 26.

Conclusion

The spread and rest operators in JavaScript provide a simple and concise way to work with arrays, objects, and function parameters. The spread operator is useful for expanding elements and copying structures, while the rest operator allows you to gather multiple values into a collection. Together, they enhance the flexibility and readability of your code and are essential tools for working with modern JavaScript.





Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java