Skip to content

Instantly share code, notes, and snippets.

@CodHeK
Created August 16, 2018 11:29
Show Gist options
  • Save CodHeK/1778414adab0503a66305824f86b4cc7 to your computer and use it in GitHub Desktop.
Save CodHeK/1778414adab0503a66305824f86b4cc7 to your computer and use it in GitHub Desktop.
rest operator
//In arrays
let nums = [1, 2, 3, 4, 5]
let [a, ...b] = nums
console.log(a) //prints 1
console.log(b) //prints [2, 3, 4, 5]
let nums = [1, 2, 3, 4, 5]
let [a, ...b, c] = nums
console.log(a) //prints 1
console.log(b) //prints [2, 3, 4, 5]
console.log(c) //error ( "SyntaxError: Rest element must be last element" )
//In Objects
let obj = {
name: ["Gagan", "Anmol"],
}
obj = {...obj, name: [...obj.name, "Mantek"] }
console.log(obj.name) // ["Gagan", "Anmol", "Mantek"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment