0Pricing
JavaScript Academy · Lesson

Rest & Spread — Idiomatic Arrays and Objects

Collect the rest of values/props, clone and merge with spread, and avoid hidden mutations with simple patterns.

Intro

Goal: Use rest to collect extras and spread to copy or merge.

  • Array/object spread
  • Array rest (head/tail)
  • Object rest (exclude some keys)
  • Mutation vs copy
Rest & Spread — Idiomatic Arrays and Objects — illustration 1

Array spread basics

[...arr] copies an array; combine arrays by spreading them in a new array.

// Array spread creates a shallow copy and merges arrays
const a = [1, 2];
const b = [3, 4];

const clone = [...a];
const merged = [...a, ...b];

console.log("a:", a);
console.log("clone:", clone);
console.log("merged:", merged);
Rest & Spread — Idiomatic Arrays and Objects — illustration 2

All lessons in this course

  1. Array/Object Destructuring Basics
  2. Rest & Spread — Idiomatic Arrays and Objects
← Back to JavaScript Academy