In previous lecture, we have discussed how spread operator works and now I want to introduce you new
type of parameter in ethics, which is the rest from syntactic perspective, they are expressed the
exact same way.
We have to use three dots, but w parameter works in an opposite way.
So let's create simple function costs a equals the function and insert here two parameters X and Y.
Let's run in council with some of those parameters, right, council log X plus Y.
Then called the function and place here more than two arguments, right, 10, 20, 30 and 40, if we
rule out, then we'll get some our first and second arguments.
Regardless of that, we have pushier more than two arguments.
Function is working without any errors or bux.
But again, we get some of only first two arguments.
So actually, it's interesting how we can use the rest of the arguments in why we have special Keywood
arguments, which represents arguments of function and returns array like object, its index based and
has the length property.
Let's at first thing counsel what arguments look like, right?
Consider log arguments.
That revolt and you see that we have an object which looks like an array.
The items have index numbers and also it has length property.
Also, as you notice, it doesn't contain only third and fourth arguments.
It includes all four arguments.
So in order to use third and fourth arguments and calculate the total sum, we can write the following
plus arguments.
And for third arguments, we have to place the index number two.
Then again, plus arguments with index number three.
Reload and we have 100, the sum of all the arguments.
As we said, arguments is an array like object, so in order to use our methods with it, we need to
transform it into array.
I'm sure you remember S5 approach, which we have seen in previous videos.
So we need to write a red DOT prototype.
Dot, dot, dot com.
So if we reload, we will get arguments collection, but in this case, as a right, I would like to
note that arguments object doesn't work with our functions.
If we change this regular function and right here our function, then we will get an error telling us
that argument is not defined.
All right, in Essex.
There is no need to use arguments, objects, because new version of JavaScript allows us to use Reath
parameter, which is more flexible and convenient than arguments.
Let's delete those lines.
And after X and Y.
Right.
Dot, dot, dot, dot.
Actually, it's not keyword you can use here anything you want, like, for example, arguments or something
else.
But remember that using three dots is always mandatory.
That's the first thing Castlebar about this parameter gives us.
Right.
Cancel that log rest.
So as you see, it returns an array, but only with two arguments, third and fourth, which actually
are missed as parameters.
And in order to gather some of these arguments, we have to write X plus Y plus rest with index number
zero and then plus rest with index number one.
So we have, again, total sum of arguments.
All right, let's use another example and see better the power of parameter.
Create consent family.
Suppose that we would have several members of family, those persons would have common last name.
So let's place you as a first parameter.
Last name.
And then let's assume that as arguments, we would have names of family members for that, I'm going
to use a rest parameter, so.
Right, dot, dot, dot names.
Then use erode function because of that the parameter returns array.
Therefore, I'm going to use a Ray Hopper method for each and using this method.
We will define first names and last names for each family member.
So.
Right.
Names for each.
Then as the current item right name, which is followed by eral function.
And then running castle.
Name and.
Last name.
Using tablet strength.
All right, let's invoke dysfunction right below family for the last time, I'm going to insult your
argument as, for example, Smith and for names of our family members.
Let's place your neck then, Jane.
Bob.
And Jessica.
Let's reload the page.
And you said that we have here persons with different names, but with common last name, Smith.
All right.
So you see how convenient and also how simple is to use race parameter.
Also, you need to remember that we always have to place a parameter at the end inside parentheses.
It doesn't matter how many arguments you will have, a parameter will work with all arguments which
are not represented with parameters.
OK, it was all about race parameter in Essex.
See you in the next video.