arguments.length

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

arguments.length 数据属性包含传递给函数的参数数量。

一个非负整数。

arguments.length 的属性特性
可写
可枚举
可配置

描述

arguments.length 属性提供了实际传递给函数的参数数量。传递的参数数量可能多于或少于定义的参数数量(参见 Function.prototype.length)。例如下面的函数:

js
function func1(a, b, c) {
  console.log(arguments.length);
}

func1.length 返回 3,因为 func1 声明了三个形式参数。然而,func1(1, 2, 3, 4, 5) 会记录 5,因为 func1 被调用时传递了五个参数。同样地,func1(1) 会记录 1,因为 func1 被调用时传递了一个参数。

示例

使用 arguments.length

在此示例中,我们定义了一个可以将两个或多个数字相加的函数。

js
function adder(base /*, num1, …, numN */) {
  base = Number(base);
  for (let i = 1; i < arguments.length; i++) {
    base += Number(arguments[i]);
  }
  return base;
}

规范

Specification
ECMAScript® 2025 Language Specification
# sec-arguments-exotic-objects

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
length

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见