Course contentsShow
← JavaScript
Lesson 962 of 2,72322. Spread and RestFree lesson

Concatenating Arrays with Spread

Combine multiple arrays into one using spread instead of the concat method.

Concatenating Arrays with Spread

You'll learn how to merge multiple arrays into one using the spread operator instead of the older concat() method.

The Core Idea

When you need to combine two or more arrays into a single array, the spread operator (...) offers a clean, readable syntax. Instead of calling .concat() on an array, you can "spread out" each array's elements inside a new array literal.

Think of it like emptying multiple boxes of puzzle pieces into one big container — the spread operator unpacks each array and places all the pieces together in a new array.

The Syntax

const fruits = ['apple', 'banana'];
const veggies = ['carrot', 'broccoli'];
const desserts = ['cake', 'pie'];

// Using spread to concatenate
const allFoods = [...fruits, ...veggies, ...desserts];
console.log(allFoods);
// ['apple', 'banana', 'carrot', 'broccoli', 'cake', 'pie']

You can even mix in individual elements:

const breakfast = ['eggs', 'toast'];
const lunch = ['sandwich', 'salad'];

const meals = ['coffee', ...breakfast, 'snack', ...lunch, 'water'];
console.log(meals);
// ['coffee', 'eggs', 'toast', 'snack', 'sandwich', 'salad', 'water']

Why Use Spread?

  • More readable: You see exactly what's being combined in one expression
  • Flexible: You can easily insert elements between arrays
  • Immutable: Creates a new array without modifying the originals
  • Consistent syntax: Uses the same ... operator you already know

Key Takeaway: Use [...array1, ...array2, ...array3] to concatenate arrays immutably — it's cleaner and more flexible than .concat(), and you can mix in standalone elements anywhere in the new array.