JavaScript Spread Operator - Exercise 1
It expands the array's elements into individual values.
const values = [1, 2, 3];
console.log(...values);
Place the existing array inside a new array literal with three dots.
const original = [1, 2];
const copy = [...original];
Put both arrays in a new array literal using spread syntax.
const first = [1, 2];
const second = [3, 4];
const combined = [...first, ...second];
No. It creates a shallow copy, so nested objects are still shared.
const source = [{ active: true }];
const copy = [...source];
Create a new array with the existing elements and the new item.
const next = [...items, 'new item'];
It copies an object's enumerable own properties into a new object.
const user = { name: 'Mia' };
const updated = { ...user, active: true };
A later property with the same key overwrites the earlier value.
const result = { ...{ role: 'user' }, role: 'admin' };
Spread the array in the function call.
const numbers = [4, 7];
Math.max(...numbers);
It consumes iterable values such as arrays and strings.
const letters = [...'cat'];
Spread expands values, while rest gathers values into an array or object.
const [first, ...remaining] = [1, 2, 3];