newsletter

Spread and Rest Operators in JavaScript

3 min read
Copied

Introduction

ECMAScript 2015 (ES6) has brought many features that have significantly improved the language's expressiveness, readability, and efficiency. Among these features, are rest and spread operators. In this blog post you will learn how to use spread and rest operators and how they can simplify your JavaScript code and make it more flexible.

Copied

Understanding the Spread Operator

The syntax of spread operator is using three dots (...), and it allows to expand an array or object into its individual elements.

Expanding arrays
Imagine you have two arrays and you want to combine them into a single array. Before ES6, you might use the concat method:

javascript
var fruits = ["apple", "orange"]; var moreFruits = ["cherry", "lemon"]; var allFruits = fruits.concat(moreFruits);

With the spread operator, this becomes much more intuitive and concise:

javascript
var fruits = ["apple", "orange"]; var moreFruits = ["cherry", "lemon"]; const allFruits = [...fruits, ...moreFruits];

Expanding objects
ES 2018 extended the spread operator to objects, allowing for shallow-cloning and property merging:

javascript
const user = { name: 'Anton', age: 30 }; const location = { country: 'Great Britain', city: 'London' }; const userProfile = { ...user, ...location };

Expanding arrays into function arguments
The spread operator can also be used to pass the elements of an array as arguments to a function:

javascript
function sum(x, y, z) { return x + y + z; } const numbers = [1, 2, 3]; console.log(sum(...numbers)); // Output: 6
Copied

Understanding the Rest Operator

While the spread operator expands an array or object into its individual elements, the rest operator does the opposite, collecting multiple elements or properties into a single array or object. It's used in function parameters and destructuring assignments.

The syntax of rest operator is using three dots (...), actually the same as spread operator.

Functions with Variable Arguments
The rest operator can be used to gather any number of arguments into an array:

javascript
function sum(...numbers) { return numbers.reduce((total, n) => total + n, 0); } console.log(sum(1, 2, 3, 4)); // Output: 10

Destructuring Assignments
In destructuring assignments, the rest operator helps in extracting specific elements while assigning the remaining ones to a variable:

javascript
const [first, ...rest] = [1, 2, 3, 4, 5]; console.log(first); // Output: 1 console.log(rest); // Output: [2, 3, 4, 5]
Copied

Summary

The spread and rest operators are powerful tools in JavaScript that offer syntax for array and object manipulation, function argument handling, and more. By understanding and utilizing these operators, you can write more concise, readable, and expressive code, making your JavaScript applications cleaner and more efficient.

Hope you find this newsletter useful. See you next time.

Whenever you're ready, here's how I can help you:

The .NET Senior Playbook is built to:

  • Fast-track you from junior or mid-level to senior
  • Keep you growing as a senior
  • Help you beat any .NET interview

Covers everything: C#, ASP.NET Core, EF Core, system design — answer each question first, reveal the solution, and a test after every chapter proves it stuck. Finish, and you earn a verifiable certificate for your LinkedIn.

The .NET Senior Playbook
View the Playbook

Not sure where you stand? Take the free .NET Interview Run:

  • Find out your real level — Junior to Senior+
  • A realistic mock .NET interview — across 13 areas of C#, .NET, ASP.NET Core and System Design

No credit card required. When you finish, you get a personalized report: your level, your strongest and weakest areas, and where to focus next — the perfect way to benchmark yourself before diving into the Playbook.

Start your free run

Enjoyed this article? Share it with your network

Improve Your .NET and Architecture Skills

Join my community of 25,000+ developers and architects.

Each week you will get 1 practical tip with best practices and real-world examples.

Learn how to craft better software with source code available for my newsletter.

Join 25,000+ developers already reading
No spam. Unsubscribe any time.