Skip to main content

Command Palette

Search for a command to run...

Spread vs Rest Operators in JavaScript: The Three Dots Explained

A simple guide to understanding when to pack and when to unpack your data.

Updated
5 min read
Spread vs Rest Operators in JavaScript: The Three Dots Explained
A

Full-stack developer and startup founder building tech solutions in Ayodhya. Vlogger with 27k+ followers sharing my journey in technology and SaaS.

If you are learning JavaScript, you have probably seen three little dots (...) floating around in the code. Sometimes they are used inside function parentheses, and other times they are used inside arrays or objects.

These three dots look exactly the same, but they actually do two completely different jobs depending on where you put them. They are called the Spread Operator and the Rest Operator.

Today, we are going to break down exactly what they do, the difference between them, and how to use them in simple English.

What is the Spread Operator? (Unpacking)

The easiest way to think about the Spread Operator is to imagine a box of toys. If you "spread" the toys, you are taking them out of the box and laying them out individually on the floor.

In JavaScript, the "boxes" are usually Arrays or Objects. The spread operator takes the items out of an array or object and expands them.

Using Spread with Arrays

A very common use case for the spread operator is combining two arrays together. Before spread, this was a bit annoying. Now, it is super easy:

const fruits = ["Apple", "Banana"];
const vegetables = ["Carrot", "Potato"];

// We use ... to take the items OUT of the old arrays 
// and put them into this brand new array!
const food = [...fruits, ...vegetables];

console.log(food); 
// Output: ["Apple", "Banana", "Carrot", "Potato"]

Using Spread with Objects

You can also do the exact same thing with objects. It is a great way to combine information or make a copy of a user profile.

const basicUser = { name: "Sarah", age: 25 };
const extraInfo = { job: "Developer", city: "New York" };

// We "spread" both objects into a new one
const fullUserProfile = { ...basicUser, ...extraInfo };

console.log(fullUserProfile);
// Output: { name: 'Sarah', age: 25, job: 'Developer', city: 'New York' }

Diagram: Spread Expanding Elements

[ Box 1: A, B ]       [ Box 2: C, D ]
       \                     /
        \                   /
     ( ...Box1 )       ( ...Box2 )
          \                 /
           v               v
    New Box: [ A, B, C, D ]
    (The items were unpacked and put together!)

What is the Rest Operator? (Packing)

If the spread operator is taking toys out of a box, the Rest Operator is doing the exact opposite: it is cleaning up the room and putting all the leftover toys into a box.

We use the rest operator when we want to collect multiple values and pack them into a single array. You will almost always see the rest operator used inside the parentheses of a function.

A Simple Function Example

Imagine you want to write a function that adds numbers together. But what if you don't know how many numbers the user is going to pass in? They might pass 2 numbers, or they might pass 10!

The rest operator saves the day. It catches all the "rest" of the arguments and packs them into a neat array for you.

// The ...numbers part packs all incoming arguments into an array called 'numbers'
function addAllNumbers(...numbers) {
  console.log("Here is the packed array:", numbers);
}

addAllNumbers(1, 2, 3, 4, 5); 
// Output: Here is the packed array: [1, 2, 3, 4, 5]

Rest with Destructuring

You can also use it when you are grabbing specific items from an array, but want to group the "rest" of the items together.

const winners = ["John", "Alice", "Bob", "Sarah"];

// We grab the first person, and pack the "rest" into a new array
const [firstPlace, ...everyoneElse] = winners;

console.log(firstPlace);    // Output: "John"
console.log(everyoneElse); // Output: ["Alice", "Bob", "Sarah"]

Diagram: Rest Collecting Values

Incoming loose items:  A, B, C, D
                          |
                     ( ...items )
                          |
                          v
         Packed into a Box: [ A, B, C, D ]

The Main Difference (Cheat Sheet)

Because they both use the ... symbol, it is easy to mix them up. Just remember this simple rule based on where you see the dots:

  • Spread: Used on the right side of an equals sign, or inside a new array/object [...x]. It unpacks things.

  • Rest: Used inside function parentheses function(...x) or on the left side of an equals sign. It packs things.

Practical Real-World Use Cases

Why do we need these tools? When you start building larger apps (especially in frameworks like React), you will use them every day!

  1. Copying Data Without Bugs: If you try to copy an array with const newArray = oldArray, modifying the new array will accidentally change the old one too! Using const newArray = [...oldArray] creates a safe, brand-new copy.

  2. Updating State: When updating a user's profile, you can use spread to keep their old info and just overwrite what changed: const updatedUser = { ...oldUser, age: 26 };

  3. Flexible Functions: As we saw earlier, the rest operator lets you build highly flexible functions that can take any amount of data without breaking.

Conclusion

The three dots ... don't have to be confusing! Just remember the toys. Are you spreading them out to play (Spread) or packing them up into a box (Rest)? Once you get the hang of it, your JavaScript code will become much cleaner and easier to read.