Celebrating 80th Independence Day of India• Jai Hind •Bharat Mata Ki Jai• Jai Hind •Long Live India• Jai Hind •Jai Hind• Jai Hind •

Spread and Rest Operator and Destructuring

Ashutosh Anand Tiwari
By Ashutosh Anand TiwariPublished May 3, 2025 · 2 min read

This is ES6 feature

Spread Operator

The spread operator is used to expand elements of an iterable, such as an array, object, or string, into individual elements.

It is used for:

  • Copying arrays
  • Merging arrays and objects
  • Passing arguments to functions
let arr1= [1,2,3]
// copy an array
let arr2 =[...arr1]
// merge an array
let mergedArray=[...arr1,...arr2]
// same goes for object
let obj1={
  name:"heyashu",
  year: 2025

}
// copy an obj 
let newObj= {...obj1}

let obj2={
 city: 'Ayodhya'

}
// merge two objects
let mergedObj= {...obj1,...obj2}

function sum(a,b){
  console.log("a is ",a)
  console.log("b is ", b)
  console.log("Sum is ",a+b)
}

const numbers=[1,3]
// passing arguments 
sum(...numbers)

console.log(arr1)
console.log(arr2)

Rest Operator

The rest operator is the opposite of the spread operator. While the spread operator expands values from an array, object, or iterable, the rest operator collects multiple values into a single data structure.

It is mainly used for receiving function arguments, array destructuring, and object destructuring.

//Function Arguments
function sum(...args){
  console.log("arg are ", args) //[ 1, 2, 3, 4, 5, 5 ]
}

let arr= [1,2,3,4,5,5]
sum(arr)

// array Destructuring
let [a,b,c,...x] =arr

console.log(a,b,c,x) //1 2 3 [ 4, 5, 5 ]

// Object Destructuring 

let obj2={
  name:'heyashu',
 city: 'Ayodhya',
 year:2025

}

let {name, ...moreKeys }= obj2
console.log("name is ", name)  //heyashu
console.log("moreKeys",moreKeys) //{ city: 'Ayodhya', year: 2025 }

Continue Exploring

Profile

Ashutosh Anand Tiwari

Follow

Writer & curator of this digital garden — open notes for learners worldwide.

Request a Topic

Want me to write notes on a course?

Tell me the course, book, or research topic you want covered. If it helps learners, I'll consider adding structured digital notes for it in the garden.

Collaborate

Have a notes collection to feature here?

Do you have open notes you want featured in this digital garden? Let me know — we can collaborate and publish them for learners worldwide.