Spread operator Flashcards

copy

1
Q

What are the 3 operations performed using spread operator

A

copy an array
Merge array
Pass multiple arguments to a function:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Rest paramerter how it is differernt from spread ?give example

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

using spread operator
Copy Array
Merge Array
Pass multiple arguments to a function

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly