Last active
June 3, 2020 19:33
-
-
Save iHani/ed02040497b49c8dd72626a337228080 to your computer and use it in GitHub Desktop.
Spread 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
/* | |
* 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} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this deepen my understanding of it, thanks.