👫 Spread

Use the spread syntax to combine objects

What is the value of property foo below?
const obj = { 
    foo: 1, 
    bar: 2, 
    baz: 3 
};
const newObj = {
    foo: 4
    ...obj,
};

Spread Syntax

The spread syntax ... is a relatively new operator that was introduced in ES2018. It provides a concise way to combine objects and arrays.

const obj = { 
    foo: 1, 
    bar: 2, 
    baz: 3 
};
const newObj = {
    foo: 4
    ...obj,
};
console.log(newObj); // { foo: 1, bar: 2, baz: 3 }

It’s also useful for combining arrays.

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = [...arr1, ...arr2];

Questions? Let's chat

Open Discord