Sometimes you are not sure of how many parameters you need to pass to a specific function. And that could be for many, many reasons. And fortunately, JavaScript has also solved this problem for people who are not sure of the number of parameters that they need to pass to a specific function that they create. So suppose that you are going to create a function that is going to take the names of all employees of a specific company, it would be impossible to pass all of these names, especially if the company has thousands of employees. Therefore, we have something called a various parameter, which is going to take an infinite number of parameters. It doesn't matter how many parameters you intend to pass the two specific function you can pass as many fun as you can.
Pass as many parameters as you want. So for example, here, I'm going to create a function function. And it's going to be employees, employees are names. and here if I wanted to pass the names of all employees, I would type for example, name one, name one, and then name two, and then Name three, and, and then name four, until, for example, name 1000, that would be absolutely tedious and impractical and a waste of time. Therefore, in order to solve this problem, you can use the rest parameter, which has a syntax of three dots, and then name and this syntax. What that means is that now you can pass as many parameters to this function as you want, and I'm going to prove it to you here.
I'm going to Use the console. Con. So that log, and here I'm going to just print the name, this parameter. And let's call this function and see what this parameter is all about is all about. So here I'm going to save and run. And as you can see, what I get is an array.
And what that means is that this parameter is an array, which means that each time you pass a parameter, it is going to be stored into this array. And you can access this array as a normal access that variable as a normal, very normal item in an array. And of course, if I use the square bracket notation and add zero, and if I save and run on run, I'm going to get undefined because this array now is empty is an empty array. But suppose that I pass here, john, as the first name, john If I save on run, I'm going to get john because I used the first index, which is zero. And john now has been added to this array to the name array as the first value or the first item. And if I just print the name array, if I save and run, I'm going to get john inside this array.
If I want to pass another parameter, I just add comma, and here, just add K. And if I save on run, I'm going to get k, john and Kate and Kate will be at the second position in this array, which means it will have an index of one. So now you can pass as many parameters to this function as you want. It doesn't even matter. All you need to do here is once you start once you call the function, you just pass the parameter that you want to pass to this function. And inside this function you can access exactly which parameter that you want to use. So in conclusion, the rest parameter is very powerful in case that you don't know how many parameters you want to pass or you are unsure till the moment of how many parameters you want to pass to your function.