codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

52 / 65 topics
50JavaScript Template Literals51JavaScript Destructuring52JavaScript Spread & Rest Operators53JavaScript Default Parameters54JavaScript Modules (import/export)55JavaScript Iterators and Generators56JavaScript Proxies
Tutorials/JavaScript/JavaScript Spread & Rest Operators
🌐JavaScript

JavaScript Spread & Rest Operators

Updated 2026-05-12
15 min read

JavaScript Spread & Rest Operators

In modern JavaScript development, the spread (...) operator is a versatile tool that allows you to expand arrays and objects or gather arguments. Understanding how to use these operators can greatly enhance your code's readability and efficiency. In this tutorial, we'll explore both the spread and rest operators in detail.

Introduction

The spread operator (...) has two main uses:

  1. Spread Operator: Used to expand elements of an iterable (like arrays or strings) into individual elements.
  2. Rest Operator: Used to gather multiple elements into a single array or object.

These operators are part of the ES6+ specification and have become essential in modern JavaScript programming.

Spread Operator

Expanding Arrays

The spread operator can be used to expand the elements of an array into individual elements, which is particularly useful for combining arrays or creating new arrays with additional elements.

Example 1: Combining Arrays

JavaScript
1// script.js
2const arr1 = [1, 2, 3];
3const arr2 = [4, 5, 6];
4const combinedArray = [...arr1, ...arr2];
5
6console.log(combinedArray);
Output

Here, the spread operator is used to add elements 4 and 5 to the end of originalArray.

Expanding Objects

The spread operator can also be used to expand the properties of an object into individual key-value pairs. This is useful for merging objects or creating new objects with additional properties.

Example 3: Merging Objects

JavaScript
1// script.js
2const obj1 = { a: 1, b: 2 };
3const obj2 = { c: 3, d: 4 };
4const mergedObject = { ...obj1, ...obj2 };
5
6console.log(mergedObject);
Output

Here, the spread operator is used to add a new property c to originalObject.

Rest Operator

Gathering Arguments

The rest operator (...) can be used in function parameters to gather multiple arguments into a single array. This is particularly useful when you're not sure how many arguments will be passed to a function.

Example 5: Summing Numbers

JavaScript
1// script.js
2function sum(...numbers) {
3return numbers.reduce((acc, num) => acc + num, 0);
4}
5
6console.log(sum(1, 2, 3)); // Output: 6
7console.log(sum(4, 5, 6, 7)); // Output: 22
Output

Here, the rest operator is used to gather all properties of user except for name into a new object called otherInfo.

Practical Example

Let's create a practical example that demonstrates both the spread and rest operators in a real-world scenario. We'll build a simple application that combines multiple arrays of user data and calculates the total age of all users.

JavaScript
1// script.js
2const users1 = [
3{ name: 'Alice', age: 25 },
4{ name: 'Bob', age: 30 }
5];
6
7const users2 = [
8{ name: 'Charlie', age: 35 },
9{ name: 'David', age: 40 }
10];
11
12// Combine user arrays using spread operator
13const allUsers = [...users1, ...users2];
14
15// Function to calculate total age using rest operator
16function calculateTotalAge(...users) {
17return users.reduce((total, user) => total + user.age, 0);
18}
19
20console.log('All Users:', allUsers);
21console.log('Total Age:', calculateTotalAge(...allUsers));
Output
All Users: [{name: 'Alice', age: 25}, {name: 'Bob', age: 30}, {name: 'Charlie', age: 35}, {name: 'David', age: 40}]
Total Age: 130

In this example, we use the spread operator to combine two arrays of user data into a single array. We then use the rest operator in the calculateTotalAge function to gather all users and calculate their total age.

Summary

  • Spread Operator: Used to expand elements of an iterable (arrays or objects) into individual elements.
  • Rest Operator: Used to gather multiple elements into a single array or object.

Both operators are powerful tools that can simplify your code and make it more flexible. By understanding how to use them effectively, you can write cleaner and more efficient JavaScript code.

What's Next?

In the next tutorial, we'll explore "JavaScript Default Parameters," which allow you to specify default values for function parameters. This feature helps improve code readability and robustness by providing sensible defaults when arguments are not supplied.


PreviousJavaScript DestructuringNext JavaScript Default Parameters

Recommended Gear

JavaScript DestructuringJavaScript Default Parameters