Created
August 16, 2018 11:29
-
-
Save CodHeK/1778414adab0503a66305824f86b4cc7 to your computer and use it in GitHub Desktop.
rest operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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