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

What is the Spread Operator?

Learn the syntax and purpose of the spread operator (...) for expanding array elements.

What is the Spread Operator?

You'll learn what the spread operator is and how it expands array elements in place.

The Core Idea

The spread operator uses three dots (...) before an array to "unpack" or "spread out" its elements individually. Think of it like opening a box of chocolates and laying each piece out on the table — instead of handling the box as a whole, you now have access to each item separately.

When you write ...myArray, JavaScript takes every element inside myArray and expands them as if you'd typed them out one by one.

Why It Exists

Arrays are containers that hold multiple values. Sometimes you need to work with those individual values rather than the container itself. The spread operator gives you a clean, readable way to expand an array's contents wherever individual values are expected.

Basic Mental Model

const numbers = [1, 2, 3];

// Without spread - you get the array itself
console.log(numbers);        // [1, 2, 3]

// With spread - you get the individual elements
console.log(...numbers);     // 1 2 3

In the second example, ...numbers is like writing 1, 2, 3 directly. The array "disappears" and only its contents remain.

Common Contexts

You'll typically use spread where JavaScript expects comma-separated values:

  • Combining arrays
  • Passing multiple arguments to functions
  • Creating copies of arrays

The spread operator makes these operations cleaner and more intuitive than older techniques.

Key Takeaway: The spread operator (...) unpacks an array's elements into individual values, letting you work with array contents flexibly wherever comma-separated values are needed.