Skip to main content
TOPIC 3 OF 8 · 2 MIN READ

Spreading

The spread operator (`...`) expands an array, object, or iterable into its individual elements. It is useful for copying, merging, or destructuring.

Key concepts

Spread Operator

The `...` operator expands an array, object, or iterable into its individual elements. Useful for copying, merging, or destructuring.

Examples

Array Spreading:

const nums1 = [1, 2, 3];
const nums2 = [4, 5, 6];
const combined = [...nums1, ...nums2];

console.log(combined); // [1, 2, 3, 4, 5, 6]

Object Spreading:

const obj1 = { name: 'Alice', age: 25 };
const obj2 = { city: 'New York' };
const merged = { ...obj1, ...obj2 };

console.log(merged); // { name: 'Alice', age: 25, city: 'New York' }

Swapping Variables:

let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b); // 2, 1

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus