Example 1 — Spread into Arguments
Turn an array into separate arguments.
const nums = [3, 1, 7];
console.log(Math.max(...nums)); How It Works
- Expand. Equivalent to
Math.max(3, 1, 7). - No loop. Spread feeds the call directly.
Spread syntax (...) expands an iterable or object’s own enumerable properties into individual elements, arguments, or fields.
const a = [1, 2];
const b = [...a, 3];
const user = { name: "Ada" };
const copy = { ...user, role: "dev" };
console.log(b.join(", "));
console.log(copy.name, copy.role); 3.role.Turn an array into separate arguments.
const nums = [3, 1, 7];
console.log(Math.max(...nums)); Math.max(3, 1, 7).Later spreads win when keys collide.
const defaults = { host: "localhost", port: 80 };
const override = { port: 3000 };
const cfg = { ...defaults, ...override };
console.log(cfg.host + ":" + cfg.port); port.Nested objects are not deep-cloned.
const original = { meta: { ok: true } };
const clone = { ...original };
clone.meta.ok = false;
console.log(original.meta.ok); meta.clone.meta also changes original.meta.Writing ...value to expand an array/iterable or object into places that expect multiple items or fields.
Spread expands. Rest gathers leftover items into an array.
No. It is a shallow copy of own enumerable properties.
CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful