Variadic Functions in JavaScript

Like C# params, JavaScript also supports variadic functions. In variadic functions the total number of parameters are unknown and can be adjusted at the time of calling the function.

In computer programming, a variadic function is a function of variable arity; that is, one which can take different numbers of arguments. Support for variadic functions differs widely among programming languages.

In the javascript the parameters can be directly accessed into the function from the "arguments".

Take below example

function PrintList()
{
  for (var i = 0; i < arguments.length; i++)
  {
    document.write(arguments[i] + "<br />");
  }
}
// Call to Variadic Function
PrintList('Google', 'Microsoft', 'Yahoo', 'Adobe');

2 comment(S)


Mike Borozdin on Jun 27, 2008 12:00 PM

Thanks :)! I didn't know about that. By the way, it resembles me PHP at this point.

Aivaras on Mar 19, 2010 01:19 AM

Thank you so much for sharing this method! It really made my day :)

Leave a comment