Default and Rest Parameters
Revisit defaults and rest on function expressions and arrows.
Phase 10 ยง58 introduced defaults and rest on classic functions. Here the same ideas appear on expressions and arrows, with a few more call patterns.
Default (arrow)
Rest after a named param
Default in the middle
Results appear here.
Important JavaScript
const heading = (title = "Untitled") => {
return "# " + title;
};
const totalWithBonuses = function (base, ...bonuses) {
let sum = base;
bonuses.forEach(function (n) {
sum = sum + n;
});
return sum;
};
const multiply = (left, right = 1) => left * right;
heading(); // "# Untitled"
heading("Practice"); // "# Practice"
totalWithBonuses(10, 5, 3);
multiply(4); // 4 * 1 โ 4
multiply(4, 3); // 12