Skip to content

Instantly share code, notes, and snippets.

@iHani
Last active June 3, 2020 19:33
Show Gist options
  • Save iHani/ed02040497b49c8dd72626a337228080 to your computer and use it in GitHub Desktop.
Save iHani/ed02040497b49c8dd72626a337228080 to your computer and use it in GitHub Desktop.
Spread Operator
/*
* Array spread operator
*/
const arr = [1, 3, 5]
arr.concat(7)
// [1, 3, 5, 7] // returns a new array, doesn't change arr
arr.push(11)
// [1, 3, 5, 11] // actually changes arr
console.log(...arr)
// 1, 3, 5, 11
console.log([...arr, 13, 42])
// [ 1, 3, 5, 11, 13, 42 ]
console.log(arr)
// [1, 3, 5, 11]
/*
* Object spread operator
*/
const obj = {
name: 'Person',
age: 55
}
console.log({ id: 123, ...obj, location : 'Jeddah' })
// {
// id: 123,
// name: "Person",
// age: 55,
// location: "Jeddah"
// }
console.log({ age: 44, ...obj }) // Will NOT override 'age'
// {age: 55, name: "Person"}
console.log({ ...obj, age: 44 }) // WILL override 'age'
// {name: "Person", age: 44}
@chongiscool
Copy link

this deepen my understanding of it, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment