DEV Community

Shashi Bhushan Kumar
Shashi Bhushan Kumar

Posted on

What is the Spread Operator in JavaScript?

The spread operator (...) is a simple but powerful feature in modern JavaScript.

It allows developers to expand elements of arrays or properties of objects into new structures.

## How It Works

The three dots (...) take values from an existing array or object and spread them into a new one.

Example with Arrays

const numbers = [1, 2, 3];
const copy = [...numbers];
Enter fullscreen mode Exit fullscreen mode

This creates a shallow copy.

We can also add new values:

const extended = [...numbers, 4, 5];
Enter fullscreen mode Exit fullscreen mode

Example with Objects

const user = { name: "John", age: 25 };
const updatedUser = { ...user, city: "Delhi" };
Enter fullscreen mode Exit fullscreen mode

This copies properties and adds new ones.


Why It Matters

The spread operator:

  • Makes code shorter and cleaner
  • Prevents accidental mutation
  • Is widely used in modern frameworks

Final Thought

The spread operator may look small, but it plays a big role in writing clean and maintainable JavaScript.

Top comments (0)