Rest parameters = ...rest allow a function work with a variable number of arguments by bundling them into an array

  • spread = expands an array into seperate elements
  • rest = bundles seperate element into an array
function openFridge(...foods){ //combine element into an array
	console.log(foods);//output: array
	console.log(...foods); //output: element
}
 
const food1 = "pizza";
const food2 = "hamburger";
const food3 = "hotdog";
const food4 = "sushi";
const food5 = "ramen";
 
openFridge(food1, food2, food3, food4, food5)
 

Example:

function sum(...numbers){ //this will convert the input into array
	let result = 0;
	for(let number of numbers){
		result += number;
	}
	return result;
}
 
const total = sum(1, 2, 3, 4, 5); //input parameters
console.log(total);
 
 
function getAverage(...numbers){
	let result = 0;
	for(let number of numbers){
		result += number;
	}
	return result / numbers.length;
}
 
const total = getAverage(1, 2, 3, 4, 5); //input parameters

another example - combine strings:

function combineString(...strings){
	return string.join(" ");
}
 
const fullName = combineString("Mr.", "Spongebob", "Squarepants", "III");
 
console.log(fullName);