Mysterious (Unlimited Parameters) Function Calling (JavaScript)

Here I am going to show you a very interesting function of JavaScript. By function declaration you will see that there is no parameter but interesting thing is you can pass any numbers of parameters that you want without declare these in first brackets.


Function Declaration :

function AddNumbers()
{
var nSum = 0;

for (var i = 0; i < arguments.length; i++)
{
nSum = parseInt(arguments[i]) + parseInt(nSum) ;
}

return nSum ;
}

Description : Here “arguments” is a mysterious list by witch you can get all that parameters that you want. Below is the process of calling that function.

Function Calling : 

var nResult  = AddNumbers(“120”, “240”, “30”);

Here parameters are “120”, “240”, “30” and in that function you will get these parameters within arguments. I mean the format is :

 arguments[0] = “120”
 arguments[1] = “240”
 arguments[2] = “30”

alert(nResult);

Result : 390

Leave a Reply