Spread operator Flashcards
copy
What are the 3 operations performed using spread operator
copy an array
Merge array
Pass multiple arguments to a function:
What is Rest paramerter how it is differernt from spread ?give example
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array
Use spread (…) when expanding an array or object.
Use rest (…) when gathering multiple arguments into an array.
OR
…nums (spread) unpacks the array [1, 2, 3, 4, 5] into individual arguments.
…rest (rest) collects remaining arguments [3, 4, 5] into an array.
function fun( a, b, …etc){
console.log(a);
console.log(b);
console.log(etc);
}
let a = 1
let b = 2
let c = 3
let d = 4
let e = 5
fun(a,b,c,d,e)
using spread operator
Copy Array
Merge Array
Pass multiple arguments to a function
copy an array
const arr = [1,2,3];
const arr_copy = […arr];
console.log(“Copy : “+ arr_copy );
============================
Merge array
let arr1 = [1,2,3]
let arr2 = [4,5,6]
let arr3 = […arr1,…arr2]
console.log(arr3);
============================
To pass multiple arguments to a function.
let arr = [1,2,3,4]
let a = 10
let b = 90
function sum(a,b,c,d,e,f){
return a+b+c+d+e+f
}
console.log(sum(a,b,…arr));