JavaScript Spread Syntax

Beginner
6 min read
Updated: Nov 2026
3 examples

Definition and Usage

Spread syntax (...) expands an iterable or object’s own enumerable properties into individual elements, arguments, or fields.

JavaScript Syntax

JavaScript
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);
Try it Editor

How It Works

  1. Array spread. Copies items into a new array and appends 3.
  2. Object spread. Copies own fields, then overrides or adds role.
  3. Shallow only. Nested objects are still shared by reference.

Examples

Example 1 — Spread into Arguments

Turn an array into separate arguments.

JavaScript
const nums = [3, 1, 7];
console.log(Math.max(...nums));
Try it Editor

How It Works

  1. Expand. Equivalent to Math.max(3, 1, 7).
  2. No loop. Spread feeds the call directly.

Example 2 — Merge Objects

Later spreads win when keys collide.

JavaScript
const defaults = { host: "localhost", port: 80 };
const override = { port: 3000 };
const cfg = { ...defaults, ...override };
console.log(cfg.host + ":" + cfg.port);
Try it Editor

How It Works

  1. Copy defaults. First spread fills the object.
  2. Override port. Second spread replaces port.

Example 3 — Shallow Copy Caveat

Nested objects are not deep-cloned.

JavaScript
const original = { meta: { ok: true } };
const clone = { ...original };
clone.meta.ok = false;
console.log(original.meta.ok);
Try it Editor

How It Works

  1. Same nested ref. Spread copied the reference to meta.
  2. Mutation shows. Changing clone.meta also changes original.meta.

Frequently Asked Questions

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

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful