-
-
Save minmaxdata/d38058728c8f9cb79646f66ed8b184ee 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 | |
* * Must install "transform-object-rest-spread" as a babel plugin | |
*/ | |
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