Function accepting any number of parameters in JavaScript

The function which accepts a variable number of arguments is also known as Variadic functions.

JavaScript provides us a variable "arguments" which we can use inside the function to traverse the list of arguments.

For example
function variableParams() { 
	for(var i = 0 ; i < arguments.length; i++) {
		console.log(arguments[i]); 
	}
}


We can call this method with any number of parameters and any type of parameter as JavaScript is loosely types language.

For example
variableParams('a','b');

variableParams(1,'a',2,'b');

variableParams('a',1,2,{j:0});
Add Comments :

Login

Register