Rest parameters and spread syntax Flashcards
function sum(a, b) {
return a + b;
}
alert( sum(1, 2, 3, 4, 5) ); // returns?
3
rest ignored
function f(arg1, …rest, arg2) { // arg2 after …rest ?!
// returns and why
}
error
spread must be at end
Arrow functions do not have ______
“arguments”
let arr = [3, 5, 1];
alert( Math.max(arr) ); // NaN
how can we fix the above to return 5
alert( Math.max(…arr) );
spread operator
let arr1 = [1, -2, 3, 4];
let arr2 = [8, 3, -8, 1];
alert( Math.max(…arr1, …arr2) ); //
8
let arr1 = [1, -2, 3, 4];
let arr2 = [8, 3, -8, 1];
alert(/* write code here */ ); // 8
how to get max number
Math.max(…arr1, …arr2)
let arr1 = [1, -2, 3, 4];
let arr2 = [8, 3, -8, 1];
alert( Math.max(1, …arr1, 2, …arr2, 25) ); // returns?
25
let arr = [3, 5, 1];
let arr2 = [8, 9, 15];
let merged = [0, …arr, 2, …arr2];
alert(merged); //
0,3,5,1,2,8,9,15
let arr = [3, 5, 1];
let arr2 = [8, 9, 15];
let merged = /* write code here */
alert(merged); // 0,3,5,1,2,8,9,15
[0, …arr, 2, …arr2];
let str = “Hello”;
alert( /* write code here*/ ); // H,e,l,l,o
[…str]
let str = “Hello”;
alert( […str] ); // H,e,l,l,o
the above is the same as______
Array.from(str)
When … is at the end of function parameters, it’s “rest parameters” and _______
gathers the rest of the list of arguments into an array.