Spread and Rest


 Spread and Rest

Spread and Rest boths are donated by (...)  three doted

USES:

 Both are use in arrya and object 

SPREAD
Spread  is use to expand the array and object.

1. Spread behave with array 


As mentioned above, the ... syntax is used to expand values. In this case, arr1 is being expanded inside arr2 using the spread operator.

2. Spread behave with object

Obj2 is expand so obj behave like as a spread operator 


Rest 
Rest Operator with array 

 1. The rest operator is used to combine multiple values into a single array. It’s especially useful when we don’t know how many arguments a function will receive.


function sum(...numbers) {
  return numbers.reduce((a, b) => a + b, 0);
}
sum(1, 2, 3); 

Rest with Object

const user = {
  name: 'Sameer',
  age: 25,
  location: 'India',
  profession: 'Developer'
};

const { name, ...rest } = user;





  





Comments

Popular posts from this blog

Shallow Copy and Deep Copy in JavaScript