语法错误:元素列表后缺少 ]

当数组初始值设定项语法某处出现错误时,会发生 JavaScript 异常 "元素列表后缺少 ]"。可能缺少右方括号 (]) 或逗号 (,)。

¥The JavaScript exception "missing ] after element list" occurs when there is an error with the array initializer syntax somewhere. Likely there is a closing square bracket (]) or a comma (,) missing.

信息

¥Message

SyntaxError: missing ] after element list (Firefox)
SyntaxError: Unexpected token ';'. Expected either a closing ']' or a ',' following an array element. (Safari)

错误类型

¥Error type

SyntaxError

什么地方出了错?

¥What went wrong?

数组初始值设定项语法在某处存在错误。可能缺少右方括号 (]) 或逗号 (,)。

¥There is an error with the array initializer syntax somewhere. Likely there is a closing square bracket (]) or a comma (,) missing.

示例

¥Examples

不完整的数组初始值设定项

¥Incomplete array initializer

js
const list = [1, 2,

const instruments = [
  "Ukulele",
  "Guitar",
  "Piano",
};

const data = [{ foo: "bar" } { bar: "foo" }];

正确的是:

¥Correct would be:

js
const list = [1, 2];

const instruments = ["Ukulele", "Guitar", "Piano"];

const data = [{ foo: "bar" }, { bar: "foo" }];

也可以看看

¥See also