The Difference Between The Spread Operator and The Rest Operator

The Difference Between The Spread Operator and The Rest Operator

Explained with practical examples .The Difference Between The Spread Operator and The Rest Operator

Introduction

Both the spread operator and the rest operator look exactly the same in JavaScript. This can be confusing if you don’t know the difference between these two operators.

In this article, we will learn about the differences between these operators with some practical examples. Let’s get right into it.

Rest parameters

Rest parameters help us to pass an infinite number of function arguments.

Here is an example:

function sum(a,b,...remaining){
     console.log(a);
     console.log(b);
     console.log(remaining);
}
sum(1,2,3,4,5,6);

// a => 1
// b => 2
// ...remaining => [3,4,5,6]

In the above example, the arguments a and b refer to 1 and 2 while the rest parameter …remaining refers to everything we passed after 1 and 2. The rest parameter in this example is represented as an array [3,4,5,6] when calling the function.

So we are using the rest operator to extract all the remaining array values and put them in an array.

web-development coding javascript programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

13 Useful JavaScript Developer Tools for Writing High-Quality Code

Today I’m going to show you 13 well-know and popular tools to write better and cleaner JavaScript code. These are tools that I’m using right now on my JavaScript developer journey. So if you want to improve the quality of your projects, this list is for you.

Top Web Development Company in India

We are the best offshore web development company in India providing web applications on Blockchain, AI & Chatbots. 15+ Yrs Exp., Save upto 60% on development.

Hire Best JavaScript Developers | JavaScript development company USA

Hire Dedicated JavaScript Developers from a top javascript development agency for your business solutions. We provide JS web developers for hire at a fixed or hourly rate.