`

Spread and Rest Operators

Spread Operator examples

// Spread
var numArray1 = [9, 8, 7, 6, 5];
var numArray2 = [29, 28, 27, 26, 25];

var studentObject1 = {
  firstName: "fName",
  lastName: "lname",
  rollno: 1236544,
  collegeName: "Chitkara University",
};

var studentObjectName = {
  firstName: "fName",
  lastName: "lname",
};
var studentObjectDetails = {
  rollno: 1236544,
  collegeName: "Chitkara University",
};
// Spread for array
var numArray3 = [...numArray1, ...numArray2];
// Spread for objects
var studentObject = { ...studentObjectName, ...studentObjectDetails };

Rest Operator examples

// Rest for array
function fooSum(item1, item2) {
  let sum = item1 + item2;
  return sum;
}

// All properties in a single items array
function foo(...items) {
  console.log(items);
}

foo(1, 2 ,3) // Ouput: [1, 2, 3]

// All properties in a single items array
function foo(item1, ...items) {
  console.log(items);
}
foo(1, 2 ,3) // Ouput: [2, 3]

// All properties in a single items array
function foo(item1, item2, ...items) {
  console.log(items);
}
foo(1, 2 ,3) // Ouput: [3]