Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Function:length

From MDC


Contents

[edit] Summary

Specifies the number of arguments expected by the function.

Property of Function
Implemented in: JavaScript 1.1
ECMA Version: ECMA-262

[edit] Description

length is external to a function, and indicates how many arguments the function expects, i.e. the number of formal parameters. By contrast, arguments.length is local to a function and provides the number of arguments actually passed to the function.

[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:

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