<b><p>Number of arguments is actualy ignored.</p>Open your console to see the output from this code!</b>
<p>Notice the new Array variable <code>arguments</code> that's available inside any function. It contains all of the arguments passed in the function call, in order. </p>
<p>We can call this function and pass any number of arguments, even if there are no parameters in the function definition itself. We iterate over the <code>arguments</code> array, check each element to see if it is or can be converted to a number, and then add it to our sum. </p>
JavaScript
/* This function will add as many numbers as are passed in.
* Note the new Array variable that's available inside
* any function. It contains all of the arguments passed
* in the function call, in order.
*/
function addNumbers(){
var sum=0;
console.log("number of arguments: "+arguments.length);
for (var i=0; i<arguments.length;i++){
if (Number(arguments[i])){
sum+=arguments[i];
}
}
return sum;
}
console.log("The sum is " + addNumbers(1, 2, 3, 4, 5, "dog"));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.