Arguments.length (Property)

The number of arguments passed to a function dictates the length of the array to hold them.

Availability:

JavaScript - 1.1
JScript - 5.5
Internet Explorer - 5.5
Netscape - 3.0
Property/method value type:Number primitive
JavaScript syntax:-myArguments.length

The number of arguments passed to a function when it is called.

The length property of the Arguments object can be inspected or used in an enumeration loop to access each argument in turn.

Even if no placeholder arguments are specified, you can still call a function and pass as many arguments to it as you like. They will be assembled into an array that you can manipulate in the way you would normally operate on any other array. You can build enumerators to process all the elements and do something with them.

You can compare this value with the arity property of the owner function object. This will allow you to determine whether the correct number of arguments was passed.

Example code:

   <SCRIPT>// Declare a function that processes a variable number of arguments
   function summate()
   {
   var total = 0;
   for(var ii=0; ii<arguments.length; ii++)
   {
   total += arguments[ii];
   }
   return total;
   }
   // Call the function
   sum = summate(1, 2, 3, 4, 5);
   document.write(sum);
   </SCRIPT>

See also:Argument, Argument list, Arguments object, Collection.length, Function.arguments[], Function.arity, Function.length

Property attributes:

ReadOnly, DontEnum.

insert figure 0101

Cross-references:

Wrox Instant JavaScript - page - 27