📖 Deeper dive reading:
Sometimes you want a function to take an unknown number of parameters. For example, if you wanted to write a function that checks to see if some number in a list is equal to a given number, you could write this using an array.
function hasNumber(test, numbers) {
return numbers.some((i) => i === test);
}
const a = [1, 2, 3];
hasNumber(2, a);
// RETURNS: trueHowever sometimes you don't have an array to work with. In this case you could create one on the fly.
function hasTwo(a, b, c) {
return hasNumber(2, [a, b, c]);
}But JavaScript provides the rest syntax to make this easier. Think of it as a parameter that contains the rest of the parameters. To turn the last parameter of any function into a rest parameter you prefix it with three periods. You can then call it with any number of parameters and they are all automatically combined into an array.
function hasNumber(test, ...numbers) {
return numbers.some((i) => i === test);
}
hasNumber(2, 1, 2, 3);
// RETURNS: trueNote that you can only make the last parameter a rest parameter. Otherwise JavaScript would not know which parameters to combine into the array.
Technically speaking, rest allows JavaScript to provide what is called variadic functions.
Spread does the opposite of rest. It take an object that is iterable (e.g. array or string) and expands, or spreads, it into a function's parameters. Consider the following.
function person(firstName, lastName) {
return { first: firstName, last: lastName };
}
const p = person(...['Ryan', 'Dahl']);
console.log(p);
// OUTPUT: {first: 'Ryan', last: 'Dahl'}