JavaScript Spread Operator - Exercise 1

It expands the array's elements into individual values.

javascript

const values = [1, 2, 3];
console.log(...values);

Place the existing array inside a new array literal with three dots.

javascript

const original = [1, 2];
const copy = [...original];

Put both arrays in a new array literal using spread syntax.

javascript

const first = [1, 2];
const second = [3, 4];
const combined = [...first, ...second];

No. It creates a shallow copy, so nested objects are still shared.

javascript

const source = [{ active: true }];
const copy = [...source];

Create a new array with the existing elements and the new item.

javascript

const next = [...items, 'new item'];

It copies an object's enumerable own properties into a new object.

javascript

const user = { name: 'Mia' };
const updated = { ...user, active: true };

A later property with the same key overwrites the earlier value.

javascript

const result = { ...{ role: 'user' }, role: 'admin' };

Spread the array in the function call.

javascript

const numbers = [4, 7];
Math.max(...numbers);

It consumes iterable values such as arrays and strings.

javascript

const letters = [...'cat'];

Spread expands values, while rest gathers values into an array or object.

javascript

const [first, ...remaining] = [1, 2, 3];