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.
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.
1 2 |
|
In the above syntax, we copy all the elements of the iterable "array" to the array2 variable.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
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.
Users can use the rest of the syntax according to the following syntax.
1 2 3 4 5 6 |
|
In the above syntax, we collect all function parameters in the params array.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Through the above example, users can clearly understand the difference between rest syntax and spread syntax.
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. |
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!