When using spread, you are expanding a single variable into more:
var abc = ['a', 'b', 'c'];
var def = ['d', 'e', 'f'];
var alpha = [ ...abc, ...def ];
console.log(alpha)// alpha == ['a', 'b', 'c', 'd', 'e', 'f'];
Run code snippetEdit code snippet Hide Results Copy to answer Expand
When using rest arguments, you are collapsing all remaining arguments of a function into one array:
function sum( first, ...others ) {
for ( var i = 0; i < others.length; i++ )
first += others[i];
return first;
}
console.log(sum(1,2,3,4))// sum(1, 2, 3, 4) == 10;
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Answer from TbWill4321 on Stack OverflowfreeCodeCamp
freecodecamp.org › news › javascript-rest-vs-spread-operators
JavaScript Rest vs Spread Operator – What’s the Difference?
September 15, 2021 - JavaScript uses three dots (...) for both the rest and spread operators. But these two operators are not the same. The main difference between rest and spread is that the rest operator puts the rest of some specific user-supplied values into a JavaSc...
Spread Operator vs Rest Parameter
Console log is a good example of the rest one for anyone struggling to think of it. You've probably written console.log(1, 2, 3, 4) etc in the past, that is to say you can pass any number of arguments into the function and it will be ok with it More on reddit.com
Spread Syntax vs Rest Parameter in ES2015 / ES6
You can refer this blog for rest / spread operator - tejassavaliya.medium.com/… 2020-10-29T17:26:18.03Z+00:00 ... Save this answer. ... Show activity on this post. ES6 has new feature three dots ... ... Here ...m is a collector, it collects the rest of the parameters. More on stackoverflow.com
spread vs rest syntax in JS, a little confused.
the teacher said that the spread operator is on the right side of the "=" This seems like a really bad way of describing things. The "rest" operator appears inside of parameter lists (like in your example), or inside a destructuring declaration (like let [a, ...b] = [1, 2, 3]). It occurs where variables are being created. The "spread" operator is used when you want to "spread"/flatten the data you have into another structure. That can occur inside argument lists (some_func(...data)), or inside of something like an array ([1, 2, 3, ...someData, 4, 5, 6]). In both cases, the placement of the = is irrelevant. More on reddit.com
spread operator (rest)
Hi rusters btw it's not "rusters", we are known as "rustaceans". More on reddit.com
00:57
Spread and Rest Operators in JavaScript #javascript #DSA ...
Differences between the REST Parameter and SPREAD ...
14:00
Spread and REST operators in Javascript - YouTube
Rest vs Spread 😮 ...They look the same! - YouTube
05:15
Difference Between Rest & Spread Operator ES6 - YouTube
03:48
Spread Operator and Rest Parameters in 4 Minutes | Level up Your ...
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Spread_syntax
Spread syntax (...) - JavaScript - MDN Web Docs - Mozilla
May 22, 2026 - Spread syntax looks exactly like rest syntax. In a way, spread syntax is the opposite of rest syntax. Spread syntax "expands" an array into its elements, while rest syntax collects multiple elements and "condenses" them into a single element. See rest parameters and rest property.
Reddit
reddit.com › r/learnjavascript › spread operator vs rest parameter
r/learnjavascript on Reddit: Spread Operator vs Rest Parameter
September 16, 2022 - So if you want to make a function that can take an indefinite or varying amount of arguments, you can do just that with the rest syntax. And then in the body of the function, you can do whatever you want with those arguments that were passed into the function, knowing that they're all contained in a single array. ... Ok, thanks. I figured out the indefinite parameters bit shortly after my post, but your explanation helps add context. ... Only array spread requires iterables.
Top answer 1 of 11
142
When using spread, you are expanding a single variable into more:
var abc = ['a', 'b', 'c'];
var def = ['d', 'e', 'f'];
var alpha = [ ...abc, ...def ];
console.log(alpha)// alpha == ['a', 'b', 'c', 'd', 'e', 'f'];
Run code snippetEdit code snippet Hide Results Copy to answer Expand
When using rest arguments, you are collapsing all remaining arguments of a function into one array:
function sum( first, ...others ) {
for ( var i = 0; i < others.length; i++ )
first += others[i];
return first;
}
console.log(sum(1,2,3,4))// sum(1, 2, 3, 4) == 10;
Run code snippetEdit code snippet Hide Results Copy to answer Expand
2 of 11
96
ES6 has new feature three dots ...
Here is how we can use these dots:
- As Rest/Collector/Gather
var [c, ...m] = [1,2,3,4,5]; // m -> [2,3,4,5]
Here ...m is a collector, it collects the rest of the parameters. Internally when we write:
var [c, ...m] = [1,2,3,4,5];
JavaScript does following
var c = 1,
m = [2, 3, 4, 5];
- As Spread
var params = [ "hello", true, 7 ];
var other = [ 1, 2, ...params ]; // other => [1,2,"hello", true, 7]
Here, ...params spreads so as to adding all of its elements to other
Internally JavaScript does following
var other = [1, 2].concat(params);
Scaler
scaler.com › home › topics › what is the difference between spread and rest operator in javascript?
Difference Between Spread and Rest Operator in JavaScript | Scaler Topics
July 7, 2024 - Although both operators in JavaScript ... lies in their names. The spread operator in JavaScript expands values in arrays and strings into individual elements whereas, the rest operator, puts the values of user-specified data into a JavaScript array....
GeeksforGeeks
geeksforgeeks.org › javascript › spread-vs-rest-operator-in-javascript-es6
Spread vs Rest operator in JavaScript ES6 - GeeksforGeeks
February 23, 2024 - Example: The rest operator gathers all arguments passed to the function after the first one (10 in this case) into an array called rest. ... The spread operator, represented by three dots (...), works by taking all the items in an array and ...