+ 2

Spread Operator (JS)

const object1 = {foo: "bar", x: 42}; const object2 = {foo: "baz", y: 5}; const merge = (...objects) => ({...objects}); let mergeObject = merge(object1, object2); console.log(mergeObject); let mergeObject1 = merge({}, object1, object2); console.log(mergeObject1); Can anyone kindly explain what is happening in the merge function or how is it working? This is the last example from spread operator section of JS course. Having a hard time over this example. Playground link: https://code.sololearn.com/WA2177a20A21 Thanks in advance.

30th Jun 2021, 1:04 PM
NBinte
NBinte - avatar
1 Odpowiedź
+ 1
In the merge function whatever you pass, it will actually be passed as a single parameter and returning the all. As you have used spread operator, you can pass as more as parameter you want. All parameters will be passed as a single one.
30th Jun 2021, 3:40 PM
Kashyap Kumar
Kashyap Kumar - avatar