Home > Web Front-end > JS Tutorial > body text

Explain the benefits of extended syntax and how it differs from the remaining syntax in ES6?

WBOY
Release: 2023-09-19 21:21:03
forward
800 people have browsed it

解释一下扩展语法的好处以及它与 ES6 中的剩余语法有何不同?

In the ES6 version of JavaScript, extended syntax was introduced as a very powerful feature. We can use extension syntax to extend an array or object into a variable of the same data type.

For example, before the spread syntax was introduced in ES6, developers used a for loop to copy all elements of one array to another array. Can you copy all the elements of one array to another by writing a linear code using extended syntax instead of writing 5 to 7 lines of code using a for loop? Yes, you heard it right!

Here we will learn different use cases of extension syntax in this tutorial. Also, we'll see how it differs from the rest of the syntax at the end of the tutorial.

Propagation syntax

Expansion syntax in JavaScript is a syntax that allows iterable objects (such as arrays or objects) to be expanded into a single variable or element.

Users can use the extension syntax to extend iterable objects according to the following syntax.

let array = [10, 40, 7, 345];
let array2 = [...array];
Copy after login

In the above syntax, we copy all the elements of the iterable "array" to the array2 variable.

Benefits of extended syntax

There are some benefits or features of using extended syntax -

  • Copy an array or object,

  • Combine arrays or objects, and

  • Pass multiple elements as function parameters.

Let’s look at different examples of each of the above features of the extension syntax.

Example

Copy array using spread syntax

In this example, we use extended synatx to copy the elements of one array to another array. You can see that the single linear code copies all array elements into array2.

<html>
   <body>
      <h2>Using the spread syntax to copy one array to another</h2>
      <div id="output"></div>
      <script>
         let output = document.getElementById("output");
         let array1 = [10, 40, 7, 345];
         output.innerHTML += "Original array: " + array1 + "</br>";
         
         // copy array using spread syntax
         let array2 = [...array1];
         output.innerHTML += "Copied array: " + array2 + "</br>";
      </script>
   </body>
</html>
Copy after login

Example

Use spread syntax to merge arrays or objects

We use the extended syntax inside array1 to merge array1 and array2 instead of using JavaScript's concat() method. Additionally, when merging two arrays, we change the order of the array elements.

<html>
<body>
   <h2>Using the spread syntax to <i> copy one array to another</i></h2>
   <div id="output"></div>
   <script>
      let output = document.getElementById("output");
      let array = [10, 40, 7, 345];
      output.innerHTML += "Array 1: " + array + "</br>";
      let array2 = ["Hi, Hello"];
      output.innerHTML += "Array 2: " + array2 + "</br>";
      array = [...array2, ...array];
      output.innerHTML += "After merging the array2 and array1: " + array + "<br/>";
   </script>
</body>
</html>
Copy after login

Example

Use extended syntax to pass multiple elements as function parameters

In this example, we create the add() function that takes three values ​​as parameters and returns the sum of all three parameters. We create an array containing three values. We have used extended syntax to pass all array elements as parameters to the add() function.

<html>
   <body>
      <h2>Using the spread syntax</h2>
      <p> Passing multiple array elements as a function argument </p>
      <div id="output"></div>
      <script>
         let output = document.getElementById("output");
      
         // function to get a sum of 3 values
         function add(param1, param2, param3) {
            return param1 + param2 + param3;
         }
         let values = [50, 54, 72];
      
         // passed array values using the spread syntax
         let result = add(...values);
         output.innerHTML += "The sum of 3 array values: " + result + "</br>";
      </script>
   </body>
</html>
Copy after login

Example

Use extended synatx to copy objects

In the following example, we create the sample_obj object, which contains different key-value pairs. Using extended syntax, we have copied all key-value pairs of sample_obj to copy_object.

Since objects are iterable, we can use extension syntax to extend objects.

<html>
   <body>
      <h2>Using the spread syntax to <i>create a copy of the object.</i></h2>
      <div id="output"></div>
      <script>
         let output = document.getElementById("output");
         let sample_obj = {
            name: "Shubham",
            age: 22,
            hobby: "writing",
         };
         let copy_object = {
            ...sample_obj,
         };
         output.innerHTML += "The values of the copy_object are " + copy_object.name + " , " +copy_object.age + " , " + copy_object.hobby +  "</br>";
      </script>
   </body>
</html>
Copy after login

Remaining syntax

In JavaScript, the syntax for remaining syntax is the same as extended syntax. We can use rest syntax to collect elements in a single array or iterable, unlike expansion using spread syntax.

Typically, developers use expansion syntax for function parameters when the total number of function parameters is undefined or optional parameters are passed.

grammar

Users can use the rest of the syntax according to the following syntax.

function func(...params){
   
   //params are the array of all arguments passed while calling the function
   
   //users can access the params like params[0], params[1], params[2], ...
}
Copy after login

In the above syntax, we collect all function parameters in the params array.

Example

In this example, we create an array of strings and pass all array elements as arguments to the mergeString() function using extended syntax.

Using the rest of the syntax, we collect all parameters of the mergeString() function in the params array. We iterate over the params array and concatenate each element of the params array into the FinalString variable.

<html>
   <body>
      <h2>Using the rest syntax to collect function parameters</h2>
      <div id="output"></div>
      <script>
         let output = document.getElementById("output");
         
         // used the rest syntax to collect params
         function mergeString(...params) {
            let finalString = "";
            
            // Iterating through the array of params
            for (let param of params) {
               finalString += param;
               output.innerHTML += "Parameter: " + param + "<br>";
            }
            output.innerHTML += "The string after merging: " + finalString;
         }
         let strings = ["Welcome", "to", "the", "TutorialsPoint!"];
         
         // used the spread syntax to pass all elements of // the strings array as an argument.
         mergeString(...strings);
      </script>
   </body>
</html>
Copy after login

Through the above example, users can clearly understand the difference between rest syntax and spread syntax.

Differences between Spread and Rest syntax in ES6:

Extended syntax is different from the rest of the syntax, which is used to collect multiple elements or attributes into an array. The extension syntax allows the extension of elements, while the rest of the syntax allows collections of elements.

Examples of using extended syntax include copying one array to another, merging two arrays, passing multiple array elements as function arguments, and copying properties of one object to another.

Examples of using the remaining syntax include collection elements, function parameters, etc.

The following table highlights the differences between expansion syntax and rest syntax in ES6 -

Extended syntax

Remaining syntax

We can use expansion syntax to expand iterable objects.

We can use rest syntax to collect elements and make all collected elements iterable.

We can use it to extend data in array or object format.

We can collect all elements in the required format.

We can use it to pass parameters inside the function.

We can use it to collect function parameters or define optional parameters.

in conclusion

Expansion syntax in JavaScript is a syntax that allows iterable objects (such as arrays or objects) to be expanded into a single variable or element.

Expansion syntax is different from the rest of the syntax. The extended syntax is useful for performing tasks such as copying arrays, merging arrays or objects, and passing multiple elements as function arguments.

The remaining syntax is useful for performing tasks such as collecting multiple elements or attributes into an array.

The above is the detailed content of Explain the benefits of extended syntax and how it differs from the remaining syntax in ES6?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!