Sunday, November 28, 2021

Rest parameter in javascript

 Rest parameter is opposite of spread operator . In rest parameter function takes an argument list and transform it into an array .

--Spread operator is used in the function call.

--While rest parameter is used in function declaration .

lets see an example,

//ES5

function isFullAge(){

 var args=Array.prototype.slice.call(arguments);   //taking the arguments and converting into an array

  args.forEach(function(cur){

     console.log((2016-cur)>=18);

  });

}

isFullAge(1990,1995,2000);

//ES6

function isFullAge6(...args){ //no need to convert transform into an array .it automatically transformed                                                   using rest parameter.

    args.forEach(cur => console.log((2016-cur)>=18));

}

isFullAge6(1990,1995,2000);




No comments:

Post a Comment

Fluent interface pattern

 public class UserConfigurationManager {     private String userName;     private String password;     private UserConfigurationManager() { ...