Using Rest Parameters1:33
prev parameter on the next iteration. So if we were to try this out, and I will log this to the console, if I switch to Chrome, sure enough, we get 6. So that's all that rest parameters do for us. You add three dots, and that's your way of saying, take all the rest of the arguments, and make them an array. So just to confirm, without the three dots, we refresh. Yeah, we're going to get numbers.reduce is not a function, because numbers in this case would be equal to, what do you think? One, of course, right? It's the first argument. So if we want all of those as an array, once again, you use the rest operator. Then you perform any calculation you need to. In this case, reduce them down to a single value. And again, what's really nice about this is, you've made the API, so to speak, as flexible as possible for the user. So they never have to think about how they pass things in. They just extend the argument list. Back to Chrome,
Rest Parameter Must Be Last4:02
you couldn't do something like this, numbers and then some other value here, right? This would never work. So if you wanted to pass your value there, obviously that wouldn't work because this is going to get the rest of everything. So you would want to make sure that your rest operator or your rest parameter occurs at the end of the list. That way, at the front, you could say foo, and then the rest of the numbers. And in this particular case, foo would be equal to 1, and then numbers would be an array of 2, 3, and value in this case. So yeah, little things you need to be aware of, but you'll figure those out on the fly pretty quickly. Now, let's get rid of this. To close up shop, yeah, let's do the quick refactor here, like we talked about earlier. And you should already know how to do this. So we can switch out the function for the arrow syntax, and we can even put this all on a single line, like this, return numbers.reduce.
Refactoring to Arrow Function4:47
And you should already know how to do this. So we can switch out the function for the arrow syntax, and we can even put this all on a single line, like this, return numbers.reduce. We're going to accept the previousValue and the current, and we will return the sum. And don't forget about the implicit return when we do it in this way. So we get rid of that, and we get the exact same thing. Or, of course, if you want to put that on its own line, that's cool too. Okay, that'll do it for this lesson. In the next video, we'll take another step.
