Revision 74796 of Function.arity

  • Revision slug: JavaScript/Reference/Global_Objects/Function/arity
  • Revision title: arity
  • Revision id: 74796
  • Created:
  • Creator: Dria
  • Is current revision? No
  • Comment

Revision Content

Summary

Specifies the number of arguments expected by the function.

Property of Function
Implemented in: JavaScript 1.2, NES 3.0

JavaScript 1.4: Deprecated.

Description

arity is no longer used and has been replaced by the length property.

arity is external to the function, and indicates how many arguments a function expects. By contrast, arguments.length provides the number of arguments actually passed to a function.

Examples

Example: Using arity

The following example demonstrates the use of arity 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

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Specifies the number of arguments expected by the function.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Property of <a href="en/Core_JavaScript_1.5_Reference/Objects/Function">Function</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.2, NES 3.0
<p>JavaScript 1.4: Deprecated.
</p>
</td>
</tr>
</tbody></table>
<h3 name="Description"> Description </h3>
<p><code>arity</code> is no longer used and has been replaced by the <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Function/length">length</a></code> property.
</p><p><code>arity</code> is external to the function, and indicates how many arguments a function expects. By contrast, <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Function/arguments/length">arguments.length</a></code> provides the number of arguments actually passed to a function.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_arity"> Example: Using <code>arity</code> </h4>
<p>The following example demonstrates the use of <code>arity</code> and <code><a href="en/Core_JavaScript_1.5_Reference/Objects/Function/arguments/length">arguments.length</a></code>.
</p>
<pre>function addNumbers(x,y){
   if (arguments.length == addNumbers.length) {
      return (x+y)
   }
   else return 0
}
</pre>
<p>If you pass more than two arguments to this function, the function returns 0:
</p>
<pre>result=addNumbers(3,4,5)   // returns 0
result=addNumbers(3,4)     // returns 7
result=addNumbers(103,104) // returns 207
</pre>
Revert to this revision