// rest parameters = (...rest) allow a function work with a variable
// number of arguments by bundling them into an array
// spread = expands an array into separate elements
// rest = bundles separate elements into an array
00:00:00 example 1
00:03:52 example 2
00:06:07 example 3
// -------- EXAMPLE 1 --------
function openFridge(...foods){
console.log(...foods);
}
function getFood(...foods){
return foods;
}
const food1 = "pizza";
const food2 = "hamburger";
const food3 = "hotdog";
const food4 = "sushi";
const food5 = "ramen";
openFridge(food1, food2, food3, food4, food5);
const foods = getFood(food1, food2, food3, food4, food5);
// -------- EXAMPLE 2 --------
function sum(...numbers){
let result = 0;
for(let number of numbers){
result += number;
}
return result;
}
function getAverage(...numbers){
let result = 0;
for(let number of numbers){
result += number;
}
return result / numbers.length;
}
const average = getAverage(75, 100, 85, 90, 50);
console.log(average);
// -------- EXAMPLE 3 --------
function combineStrings(...strings){
return strings.join(" ");
}
const fullName = combineStrings("Mr.", "Spongebob", "Squarepants", "III");
console.log(fullName);
// number of arguments by bundling them into an array
// spread = expands an array into separate elements
// rest = bundles separate elements into an array
00:00:00 example 1
00:03:52 example 2
00:06:07 example 3
// -------- EXAMPLE 1 --------
function openFridge(...foods){
console.log(...foods);
}
function getFood(...foods){
return foods;
}
const food1 = "pizza";
const food2 = "hamburger";
const food3 = "hotdog";
const food4 = "sushi";
const food5 = "ramen";
openFridge(food1, food2, food3, food4, food5);
const foods = getFood(food1, food2, food3, food4, food5);
// -------- EXAMPLE 2 --------
function sum(...numbers){
let result = 0;
for(let number of numbers){
result += number;
}
return result;
}
function getAverage(...numbers){
let result = 0;
for(let number of numbers){
result += number;
}
return result / numbers.length;
}
const average = getAverage(75, 100, 85, 90, 50);
console.log(average);
// -------- EXAMPLE 3 --------
function combineStrings(...strings){
return strings.join(" ");
}
const fullName = combineStrings("Mr.", "Spongebob", "Squarepants", "III");
console.log(fullName);
- Category
- Bro Code
- Tags
- javascript, javascript tutorial, javascript full course
Be the first to comment


