JavaScript Spread and Rest

Use spread syntax to expand arrays and objects, and rest parameters to handle variable function arguments.

TL;DR

  1. 01Use ... to expand array elements or object properties into context.
  2. 02Gather remaining variables or function arguments using the rest syntax.
  3. 03Ensure rest parameters reside at the end of argument signatures.

Tips

  1. 01Leverage object spread syntax to create shallow copies and merge multiple objects without mutating originals.
  2. 02Combine rest parameters and array destructuring to extract specific list elements and collect the rest.

Warnings

  1. 01Remember that object spread performs a shallow copy, leaving nested object references shared between copies.
  2. 02Placing a rest parameter before other parameters in a function signature throws a SyntaxError.

Spread with Arrays

    Array merging

    Combines elements of multiple arrays into a new array literal context.

    const arr1 = [1, 2];
    const arr2 = [3, 4];
    const merged = [...arr1, ...arr2]; // [1, 2, 3, 4]
    Array copy

    Creates a shallow copy of an array, breaking the original reference link.

    const original = [1, 2, 3];
    const copy = [...original];
    Function arguments

    Expands array items into individual parameters for function execution calls.

    const numbers = [5, 10, 3];
    Math.max(...numbers); // 10
    Iterable spread

    Converts strings or Sets into arrays using the spread operator.

    const chars = [..."hi"]; // ["h", "i"]

Spread with Objects

    Object merging

    Merges property fields of multiple objects into a new object container.

    const user = { name: "Alice", age: 30 };
    const updated = { ...user, active: true };
    Property override

    Applies new values to keys by placing overrides after the spread target.

    const base = { role: "user", id: 10 };
    const admin = { ...base, role: "admin" };
    Shallow constraints

    Spreads only top-level fields, leaving nested objects pointing to shared references.

    const obj = { nested: { val: 1 } };
    const copy = { ...obj }; // copy.nested is shared

Rest Parameters

    Argument gathering

    Collects excess function arguments into a single standard array handle.

    function sum(...numbers) {
      return numbers.reduce((a, b) => a + b, 0);
    }
    Named plus rest

    Combines initial named parameters with trailing rest parameter collections.

    function greet(message, ...names) {
      console.log(`${message} ${names.join(", ")}`);
    }
    Array destructuring

    Gathers remaining array items into a slice list during value assignment.

    const [first, ...rest] = [1, 2, 3, 4];
    // first = 1, rest = [2, 3, 4]
    Object destructuring

    Extracts target properties while collecting remaining fields in a separate object.

    const { password, ...safeData } = user;
    // password is isolated, rest goes to safeData

Spread vs Rest

    Context direction

    Spread expands collections out, while rest collects free elements in.

    const arr = [1, 2];
    const spread = [...arr]; // expands elements
    const [...rest] = arr; // collects elements
    Usage locations

    Spread occurs in literals and calls; rest occurs in signatures and destructuring.

    Math.min(...[1, 2]); // spread in call
    function test(...args) {} // rest in signature

In Practice

FAQ