Arguments and Rest parameter in Javascript

Arguments

Argument is an array like variable in JavaScript, which can be accessible in function that can be used to extract the value of the arguments passed to the function.

for example :

function testArguments(){

    console.log(arguments); //[object Arguments] { 0: 1,1:2,2:3} 

    console.log(arguments.length); // 3

    console.log(arguments[0]); // 1

    console.log(arguments[1]); // 2

    console.log(arguments[2]); // 3

    console.log(typeof(arguments)); // "object"

}

testArguments(1,2,3);

Before the introduction of rest parameter , arguments variable was used to extract all the arguments of the function. Arguments is array like object but it does not support array.map(...) but we can iterate using for loop. Let's  simplified above example with for loop.

example : using array.map

function testArguments(){

 arguments.map(function(item){

    console.log(item);

        });

    }


testArguments(1,2,3);

Above code produce "Uncaught TypeError: arguments.map is not a function", as it does not support array.map(...).

example : using for loop

function testArguments(){

    for(a=0;a<arguments.length;a++){

    console.log(arguments[a]);

     }

   }

testArguments(1,2,3);


Output of above example will : 1 2 3

Also arguments is not available in array function. Let's explore with an example :

testArguments  = ()=>{

console.log(arguments[a]);

}

testArguments(1,2,3);

above code produce an error : "Uncaught ReferenceError: arguments is not defined".


Also arguments always capture all arguments, you can not use partially. for example : 

function testArguments(a,b){

    console.log(a); // 1

            console.log(b); //2

        console.log(arguments[0]); // 1

            console.log(arguments[1]);//2

              console.log(arguments[2]);//3

       }

testArguments(1,2,3); 


From above examples, we can conclude that there are some limitation of arguments variable , as arguments is not iterable using array.map(...) , can not be used to extract partial arguments and it is not available in arrow function. 

So to overcome these limitation rest is preferable.

Rest Parameter

Rest parameter, allow to extract indefinite number of arguments as an array that are passed to the function.

Rest parameter can be included using three dots ... followed by name of the array in function definition.

example : 

function testRestParameters(...restParmeter){
    console.log(restParmeter); // [1,2,3]
      console.log(restParmeter.length); // 3
}

testRestParameters(1,2,3);

Rest parameter can iterated using array.map(...)


for example : 

function testRestParameters(...restParmeter){      

      restParmeter.map((item)=>{

      console.log(item);

      })

}

testRestParameters(1,2,3);


Output  : 1 2 3


Rest parameter can be used to extract partial value of the arguments.

example : 

function testRestParameters(a,b,...restParmeter){ 

console.log(a);

  console.log(b);

      restParmeter.map((item)=>{

      console.log(item);

      })

}


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

output : 1 2 3 4 5


Limitation of rest parameter

Rest parameter must be at the end of the arguments.

example :

function testRestParameters(a,b,...restParmeter,c){ 
console.log(restParameter);
}

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

above code produce : "Uncaught SyntaxError: Rest parameter must be last formal parameter"


Hurray!!!

#arguments
#rest parameter
#javascript







Comments