Monday, December 12, 2022

JavaScript Rest Parameter

The JavaScript rest parameter syntax allows a function to accept variable number of arguments (zero or more) as an array. It is similar to varargs in Java.

Rest parameter syntax

Rest parameter is specified using three dots (…)

function myFunc(a, b, ...restParam) {  
  // block of statements 
}  

If you have a rest parameter in your function along with other parameters then it has to be the last argument because it has to collect the remaining arguments into an array.

Rest parameter syntax (…) is similar to spread operator in JavaScript but they do opposite job. In case of spread operator array is expanded where as in case of rest parameter arguments are collected into an array.

Rest parameter JavaScript example

A typical example is to do any mathematical operation like sum. If you write a normal function with two parameters, it will add two parameters only.

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

console.log(sum(1, 2)); // 3	
console.log(sum(1, 2, 3, 4)); //No error, but function returns 3 

As you can see when the function is called with more parameters, it still takes the first 2 and returns the sum of those arguments. If you want your sum function to be more generic which can work with any number of parameters then rest parameter gives you that functionality.

function sum(...args) {
  var s = 0;
  for(let x of args){
    s += x;
  }
  return s
}
	
console.log(sum(1, 2)); // 3

console.log(sum(1, 2, 3, 4));  //10

Rest parameter with other parameters

You can have other parameters in your function along with rest parameter but in that case rest parameter should be the last one. For example, in the following function there are two parameters and then a rest parameter.

function myFunc(a, b, ...restParam)

Calling this function like this myFunc(1, 2, 3, 4, 5) maps the first two arguments to a and b and other three parameters are passed as any array to restParam.

function myFunc(a, b, ...restParam) {
  console.log("a ", a); //a  1
  console.log("b ", b); // b  2
  console.log("Rest of the params ", restParam); // Rest of the params  (3) [3, 4, 5]
}

myFunc(1, 2, 3, 4, 5);

Rules for using Rest parameter in JavaScript

  1. One of the rules as already showed is that rest parameter must be the last parameter of the function.
    function myFunc(...restParam, a, b) {} // Wrong function definition
    
  2. A function definition can only have one rest parameter.
    function myFunc(...firstParam, ...secondParam) {} // Wrong function definition
    

That's all for this topic JavaScript Rest Parameters. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. JavaScript Array and Object Destructuring
  2. JavaScript let and const With Examples
  3. JavaScript Arrow Function With Examples
  4. JSX in React

You may also like-

  1. First Java Program - Hello World Java Program
  2. Dependency Injection in Spring Framework
  3. HashMap in Java With Examples
  4. Format Date in Java Using SimpleDateFormat

No comments:

Post a Comment