DEV Community

Cover image for Understanding the Spread Operator(...) in JavaScript

Understanding the Spread Operator(...) in JavaScript

Brandon Damue on February 02, 2023

In today's episode of talking about awesome JavaScript features, we are going to turn our attention to the Spread operator(...) or Spread syntax. A...
Collapse
 
bcostaaa01 profile image
Bruno

Really great article, @brandonbawe!πŸ‘

I found it very nice that you gave a list of points where the spread operator is useful πŸ‘Œ

One thing I would add would be, in the Math functions section, that you can use the spread operator to pass array elements as arguments to a function in more use cases. One example would be calling a function with an array of arguments:

function sum(a, b, c, d) {
    return a + b + c + d;
}

let arr = [1, 2, 3, 4];
let result = sum(...arr);
console.log(result); // 10
Enter fullscreen mode Exit fullscreen mode

This is the same as if you would be calling the sum function with (1, 2, 3, 4) as β€œhardcoded” arguments.

Keep it up!πŸ™Œ

Collapse
 
brandondamue profile image
Brandon Damue

Thank you for your input Bruno. It is highly appreciated.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
brandondamue profile image
Brandon Damue

Thank you for you review Abhay. The Spread operator is really an awesome feature that makes JavaScript more fun to work with.

Collapse
 
mcsee profile image
Maxi Contieri

nice examples!

Collapse
 
brandondamue profile image
Brandon Damue

Thank you Maxi

Collapse
 
neumatic_78 profile image
neu-ma-tic

spread is good but i find does not work recursively. try lodash merge function

Collapse
 
brandondamue profile image
Brandon Damue

Okay I'm going to try that out. Thank you for your input.

Collapse
 
mahbubdev1 profile image
Munir Uddin Mahbub

yes, It's Good for me , Thank you so much

Collapse
 
brandondamue profile image
Brandon Damue

You are welcome Munir.

Collapse
 
munyanezaarmel profile image
Munyaneza Armel

Thank you for sharing

Collapse
 
9opsec profile image
9opsec

Good explanation of something that has been confusing for me!

Collapse
 
tejus07 profile image
Tejus Sahi

That's Great Article, i learned something new about spread operator use case.
Math.min and Math.max i think its really useful.

Collapse
 
brandondamue profile image
Brandon Damue

Thank you for your review sir.