The spread operator allows us to expand the elements of an array or the properties of an object into another array or object, respectively.
constcountries= ["au","nz","sg","jp","us"];// console.log(countries[0], countries[1], countries[2], countries[3], countries[4])console.log(...countries)// can be used to create a shallow copy of the original arrayconstcountriesCopy= [...countries]constscores= [29,24,28,92,1]constmoreScores= [2,...scores,32,88]console.log(moreScores)
constjohn= { name:"john", location:"iceland", year:2003 }// This operator can be used for creating a shallow copy of the original object.// const cloneOfJohn = Object.assign({}, john)constcloneOfJohn= { ...john }// Combine two objectsconstprogrammer= { experience:5, language:"snake python" }constjohnTheProgrammer= { ...john,...programmer }
Rest
In function definitions, the rest operator allows us to accept an unknown number of arguments, which are stored in an array.