What is spread, default and rest parameters in JavaScript ?
Last Updated :
13 Apr, 2022
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 example, the first function gives result 7 whereas the second function call will be "undefined" as we did not pass any second argument.
JavaScript
<script>
function add(a, b) {
return a + b
}
console.log(add(5, 2)); // 7
console.log(add(5)); // NaN
</script>
Output:
7
NaN
Example 2: In this example, we use default parameters in which we generally give a default value if no argument is provided. We take a default value of "b" so that in the second function call, we are not providing any argument, and its default value is taken.
JavaScript
<script>
function add(a, b = 3) {
return a + b
}
console.log(add(5, 2))
console.log(add(5))
</script>
Output:
7
8
Spread Operator: It is another operator provided through ES6 it generally spreads data of array/list.
In the following example, we are calculating min of all the numbers
Example 1:
JavaScript
<script>
console.log(Math.min(1, 2, 3, -1));
</script>
Output:
-1
Example 2: Consider that we have an array instead of a list then the above min() function will not work and it will give "NaN".
JavaScript
<script>
// Without spread operator
let arr = [1, 2, 3, -1];
console.log(Math.min(arr)); // NaN
</script>
Output:
NaN
Example 3: When ...arr is used, it generally spreads the arr values in the min() function.
JavaScript
<script>
// Spread operator
let arr = [1, 2, 3, -1];
console.log(Math.min(...arr)); // -1
</script>
Output:
-1
Rest Operator: It allows a function to accept an indefinite number of arguments if we are not sure how many arguments will receive.
Syntax:
function f(a, b, ...args) {
...
}
Example: In the below example, we are using the rest parameter which allows taking indefinite parameters.
JavaScript
<script>
function myFun(a, b, ...manyMoreArgs) {
console.log("a", a)
console.log("b", b)
console.log("manyMoreArgs", manyMoreArgs)
}
myFun("one", "two", "three", "four", "five", "six");
</script>
Output:
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
Spread vs Rest operator in JavaScript ES6 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
2 min read
What is the Difference Between Arguments object and Rest parameters in JavaScript? The arguments and Rest Parameters is crucial for the writing efficient and modern JavaScript code. The arguments object has been traditionally used in functions to the access the parameters passed to them. However, with the introduction of the ES6 Rest Parameters provide the more readable and flexib
2 min read
How to set default parameter value in JavaScript functions ? In general all the JavaScript functions have a default value of undefined but we can overwrite it by specifying a particular value. This prevents breaking of the code in case of missing inputs. Let us first take a look at a problem and then we'll take a look at how default values for function parame
2 min read
How to retrieve GET parameters from JavaScript ? In order to know the parameters, those are passed by the âGETâ method, like sometimes we pass the Email-id, Password, and other details. For that purpose, We are using the following snippet of code. When you visit any website, ever thought about what the question mark '?' is doing in the address bar
2 min read