In the previous video, we talked about the structure in this video.
Let's focus on risk parameters.
Risk parameters are a way to pass an arbitrary number of elements to a function, which means that we
can pass an indeterminate element to a function if this particular definition is not clear to you.
Let's demonstrate something here.
Let's say that we have this particular institution.
And this institution.
It takes, let's say, 20 applicants.
So after 20 applicants, this particular school cannot accept any incoming application.
And let's say that we have, let's say, 40 or more students left.
Good.
But with rest parameters.
What is saying is that.
Okay.
Well, we are saying that we are taking 20 applicants.
But any applicant who applies, we will still what accommodates you into the school.
So if we take this in code, we can say that the school is a function.
And then the applicants are called the agreement.
So inside this coup, it hosted all the applicants.
So this applicants are called the agreement.
So with the rest parameter.
We have access to the parameters we are requesting and any additional parameters being passed into the
function.
So let's get back to the slide and let's take this one to as an example, let's say that this is also
a function, right?
So this function, let's say it takes, let's say five or ten users, but after it has finished taking
the required users, we still have some users which has left.
So with rest parameters, we can still pass this user into the function and the function can have access
to the rest users.
So as the name implies rest, it takes the rest of the argument and the method using the red dot.
So let's get back to visual ID code and demonstrate that.
Well, so here let me comment and call this one as risks parameters.
To better understand risk parameters, we need to understand what is called engagement.
So let me demonstrate that one for you to see.
Let's say we have a function that adds two numbers.
To see two numbers, and this one would take in A and B.
So inside this function, I have access to what is called the argument.
So if I call the function the at two numbers and let's see the equipment that we have inside the function.
Let me enlarge it.
If we see that this looks like an array because we have the square bracket here and the close in here
is that.
But it's not an array where the argument object is not a real array while rest parameters are an array.
So I'll show you very soon.
So take note that the argument is not real array.
So anything that we pass to this function will be available inside the argument.
So let's pass in here.
Let's say 20 here.
If I save it, we see that it has added inside my argument and let's say I want at ten we see that it's
been added inside my argument.
Okay, so anything that you pass into the function as an argument would be available inside this argument
property.
That's one thing to bear in mind.
Again, this is not rare array, meaning we cannot use some of the methods on this particular argument.
So now you know what argument is.
It holds all the arguments we pass to this particular function.
Now let's talk about rest.
So now inside this function, how can we make use of res to rest is like an improvement on the argument.
Okay, before I show you the rest, let's try to loop through this particular argument here.
VERRASTRO We can still have some methods, but ideally is not real every so we never use that one by
using some method on that.
That is why rest comes into play.
So let's say let me remove this from here.
And for rest parameters we bring in dot, dot, dot and then any variable we can make it.
So let's say the arguments, the ag or the arguments or rest.
Is that so?
If I console.log.
Rest.
Now you could see that this is perfect array.
So it means that we can have access to any variable that we pass into this function and to pack them
or put them inside an array like that.
That is good, right?
So with this one, I can add as many arguments with this particular function I can pass in, let's say
even me a string and then to be added into the array.
And this is crucial.
Let's say that this particular function takes in two arguments.
Now we have a function with rest parameter.
What about a function with argument and with rest parameter?
Before I would demonstrate to you the logic behind this function.
Let's go ahead and create a function that would take in our own arguments and then rest with rest parameter.
So now let me copy this one and comment this one out and come down here.
Now here we can pass it A and B like that.
Now we know this one holds the.
So here I can have access to my A and then B and I have two and 20.
You can see that it couldn't print out the three and me because according to this implementation, it
says that this function needs two arguments.
So we take in 20 and ten, as we see here.
But to have access to extra argument we pass in, we have access to it by making of comma and dots the
ax.
The argument or risk it can be anything race or the and so most times make use of race or the arguments.
So now I have this.
So if I console.log the rest.
Let's see.
The difference is if I console.log.
The rest.
Now.
It's wrists.
Yes, you can see that.
Now, we have two things happening here.
We have the original agreement, which is 220 and ten as that.
But for the rest, it has taken the rest of agreements and then put them inside an array that is key
nutrient bearing in mind the rest agreements, take the rest of agreements and put them inside an array.
And remember, one function can take in only one rest parameter.
I'm thinking, what about a can I add?
Let's say another rest.
Let's say dot, dot, dot.
Let's say raise to.
Now see that?
Got an arrow.
So a function takes in only one race parameter.
Let's create a simple function that would take in some arguments and find which one is the maximum.
So the function would go like this find max or the maximum.
And this function, let's say I want to find three numbers.
Let's say A, B and C as a C note, three, C as that.
And now I'm going to use map.
Moth dart.
Maximum.
Then I'll pass in A, B and C, so let me console.log this one and let's see, just console.log it.
Okay.
So now if I call the function called find max maximum.
Now pass in, let's say ten, 20 and 30.
Which one is the highest?
So if I save it, I get 30 because 30 is the highest among this.
So what about if I add, let's say 100 into it number?
So I still see 30 because it didn't take this one into consideration.
So to have access to this one, I'm going to use the rest parameter.
Remember, the rest parameter will take the individual record and put it inside an array.
So it is I'm going to be so inside my argument here.
Instead of using some static arguments here, I'm going to use the dots.
Let's see the rest of the numbers.
And inside here, I'm going to use the same syntax as dot, dot, dot risk, meaning I'm taking the
argument from here and passing to this particular method or function if I save it by list.
Now see that I get 102.
So let's see.
I want to add let's say 200 and let's see, I could see that now we have it as 200 because now because
of risk, it is taking all the agreement you pass in and then pass into this maximum function.
That is how race works.