Spread vs Rest operator in JavaScript ES6 Last Updated : 23 Feb, 2024 Comments Improve Suggest changes Like Article Like Report Rest and spread operators may appear similar in notation, but they serve distinct purposes in JavaScript, which can sometimes lead to confusion. Let's delve into their differences and how each is used. Rest and spread operators are both introduced in javascript ES6. Rest OperatorThe rest operator is represented by three dots (...). When used in a function's parameter list, it catches any extra arguments passed to the function and packs them neatly into an array. This allows functions to handle varying numbers of arguments without explicitly defining them. So, you can think of it as a way to gather up the remaining arguments into an array for easy handling inside the function. Example: The rest operator gathers all arguments passed to the function after the first one (10 in this case) into an array called rest. JavaScript function abc(a, ...rest) { return rest; } console.log(abc(10, 1, 2, 3, 4, 5)); Output[ 1, 2, 3, 4, 5 ] Spread OperatorThe spread operator, represented by three dots (...), works by taking all the items in an array and spreading them out, essentially unpacking the array so that each item becomes an individual element. It's like taking a bunch of items from a box and laying them out separately on a table. This makes it easier to work with the individual items rather than dealing with the entire array as a single entity. Example: This example shows the use of SPread Operator JavaScript function abc(a, b, c) { return a + b + c; } console.log(abc(...[1, 2, 3])); Output6 DIfference between Spread Operator and Rest OperatorFeature Spread Operator Rest Operator Syntax Denoted by three consecutive dots (...) Denoted by three consecutive dots (...) Usage Used to expand an iterable into individual elements Used in function parameters to gather arguments Example [...array] function functionName(...args) { ... } Parameter Position Can be used anywhere in an array literal or function call Must be the last parameter in a function's parameter list Return Value Creates a new array with copied elements Note: See the above explanation Gathers remaining arguments into an array Note: See the above explanation Mutability Does not modify the original array or object Note: See the above explanation Gathers arguments into an array without altering them Note: See the above explanation Use Cases Concatenating arrays, passing arrays as arguments Capturing an arbitrary number of function arguments Comment More infoAdvertise with us Next Article Spread vs Rest operator in JavaScript ES6 C ctguy Follow Improve Article Tags : JavaScript Web Technologies ES6 Similar Reads Rest Parameter And Spread Operator in JavaScript JavaScript introduced the Rest Parameter and Spread Operator in ES6 to make handling functions and arrays more flexible and concise. These operators use the same syntax (...), but they serve different purposes. The rest parameter collects multiple values into an array, while the spread operator spre 6 min read JavaScript Spread Operator The Spread operator (represented as three dots or â¦) is used on iterables like array and string, or properties of Objects, to expand wherever zero or more elements are required to be copied or assigned.1. Adding Multiple Elements Using Spread OperatorEven though we get the content on one array insid 4 min read What is spread, default and rest parameters in JavaScript ? The default, spread, and rest parameters were added in ES6. Default Parameter: It is used to give the default values to the arguments, if no parameter is provided in the function call. Syntax: function fnName(param1 = defaultValue1, ..., paramN = defaultValueN) { ... } Example 1: In the below exampl 2 min read Arrow operator in ES6 of JavaScript ES6 has come with various advantages and one of them is the arrow operator. It has reduced the function defining code size so it is one of the trending questions asked in the interview. Let us have a deeper dive into the arrow operator's functioning. Syntax: In ES5 a function is defined by the foll 4 min read How Spread Operator Works in JS The spread operator (...) in JavaScript is a powerful feature used to expand or spread elements of an iterable (like an array or object) into individual elements. It is commonly used in situations where you want to copy or merge arrays, objects, or even pass arguments to functions.1. Arrays with the 3 min read Like