-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
59 lines (40 loc) · 1.59 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
---
title: arguments.length
slug: Web/JavaScript/Reference/Functions/arguments/length
page-type: javascript-instance-data-property
browser-compat: javascript.functions.arguments.length
---
{{jsSidebar("Functions")}}
The **`arguments.length`** data property contains the number of arguments passed to the function.
## Value
A non-negative integer.
{{js_property_attributes(1, 0, 1)}}
## Description
The `arguments.length` property provides the number of arguments actually passed to a function. This can be more or less than the defined parameter's count (see {{jsxref("Function.prototype.length")}}). For example, for the function below:
```js
function func1(a, b, c) {
console.log(arguments.length);
}
```
`func1.length` returns `3`, because `func1` declares three formal parameters. However, `func1(1, 2, 3, 4, 5)` logs `5`, because `func1` was called with five arguments. Similarly, `func1(1)` logs `1`, because `func1` was called with one argument.
## Examples
### Using arguments.length
In this example, we define a function that can add two or more numbers together.
```js
function adder(base /*, num1, …, numN */) {
base = Number(base);
for (let i = 1; i < arguments.length; i++) {
base += Number(arguments[i]);
}
return base;
}
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Functions](/en-US/docs/Web/JavaScript/Guide/Functions) guide
- [Functions](/en-US/docs/Web/JavaScript/Reference/Functions)
- {{jsxref("Functions/arguments", "arguments")}}
- [`Function`: `length`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length)