Skip to content

PRO TIP Block youtube.com at the DNS level — Pi-hole, NextDNS or your hosts file — but allow youtube-nocookie.com and i.ytimg.com. These tutorials keep playing; the rabbit hole does not.

Learning without distractions

JavaScript REST PARAMETERS in 8 minutes! 🗄

58.8K views on YouTube

// 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);