Okay, a quick reminder on Spread Syntax
If you have a Javascript function that you call with
average(1, 2, 3);
function average(x, y, z){
// Average of the three numbers
}
You are limited to just three properties/arguments. Not very useful if you have more or less numbers you want to find an average. Using Spread Syntax we don’t have to worry about the number of values passed
function average(...args) {
var sum = 0;
for (let value of args) {
sum += value;
}
return sum / args.length;
}
average(2, 3, 4, 5); // 3.5
The Spread Syntax ( the three dots{name}) includes all the unknown values. It gets better still
var averageMethod = 'mode'; var roundTo = 2; /Decimal places doThing( averageMethod, [2,4,3,6,10,19], roundTo); function doThing(method, ...valuesToAverage, roundTo) { // Calculate averages }
And Spread Syntax can be used when creating arrays
var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes'];
// ["head", "shoulders", "knees", "and", "toes"]
Good eh?