Visit Mozilla.org

Core JavaScript 1.5 Reference:Functions:arguments:length

From MDC


Contents

[edit] Summary

Specifies the number of arguments passed to the function.

Property of arguments; Function.arguments (deprecated)
Implemented in: JavaScript 1.1

JavaScript 1.4: Deprecated length as a property of Function.arguments, retained it as a property of a function's local arguments variable.

ECMA Version: ECMA-262

[edit] Description

length is a property of the arguments local variable available within all function objects; length as a property of Function.arguments is no longer used. (Function.arguments itself is also deprecated.)

arguments.length provides the number of arguments actually passed to a function. By contrast, the Function.length property indicates how many arguments a function expects.

[edit] Examples

[edit] Example: Using Function.length and arguments.length

The following example demonstrates the use of Function.length and arguments.length.

function addNumbers(x,y){
   if (arguments.length == addNumbers.length) {
      return (x+y)
   }
   else return 0
}

If you pass more than two arguments to this function, the function returns 0:

result=addNumbers(3,4,5)   // returns 0
result=addNumbers(3,4)     // returns 7
result=addNumbers(103,104) // returns 207

[edit] See also

Function.length