语法错误:参数列表后缺少 )

当函数调用方式出现错误时,就会出现 JavaScript 异常 "参数列表后缺少 )"。这可能是拼写错误、缺少运算符或未转义的字符串。

¥The JavaScript exception "missing ) after argument list" occurs when there is an error with how a function is called. This might be a typo, a missing operator, or an unescaped string.

信息

¥Message

SyntaxError: missing ) after argument list (V8-based & Firefox)
SyntaxError: Unexpected identifier 'x'. Expected ')' to end an argument list. (Safari)

错误类型

¥Error type

SyntaxError

什么地方出了错?

¥What went wrong?

函数的调用方式存在错误。例如,这可能是拼写错误、缺少运算符或未转义的字符串。

¥There is an error with how a function is called. This might be a typo, a missing operator, or an unescaped string, for example.

示例

¥Examples

因为没有 "*" 运算符来连接字符串,所以 JavaScript 期望 log 函数的参数只是 "PI: "。在这种情况下,它应该以右括号结束。

¥Because there is no "+" operator to concatenate the string, JavaScript expects the argument for the log function to be just "PI: ". In that case, it should be terminated by a closing parenthesis.

js
console.log("PI: " Math.PI);
// SyntaxError: missing ) after argument list

你可以通过添加 + 运算符来更正 log 调用:

¥You can correct the log call by adding the + operator:

js
console.log("PI: " + Math.PI);
// "PI: 3.141592653589793"

或者,你可以考虑使用 模板文字,或利用 console.log 接受多个参数的事实:

¥Alternatively, you can consider using a template literal, or take advantage of the fact that console.log accepts multiple parameters:

js
console.log(`PI: ${Math.PI}`);
console.log("PI:", Math.PI);

未终止的字符串

¥Unterminated strings

js
console.log('"Java" + "Script" = \"' + "Java" + 'Script\");
// SyntaxError: missing ) after argument list

这里 JavaScript 认为你的意思是在字符串中包含 ); 并忽略它,并且最终它不知道你的意思是 ); 来结束函数 console.log。为了解决这个问题,我们可以在 "脚本" 字符串后面放置 a'

¥Here JavaScript thinks that you meant to have ); inside the string and ignores it, and it ends up not knowing that you meant the ); to end the function console.log. To fix this, we could put a' after the "Script" string:

js
console.log('"Java" + "Script" = "' + "Java" + 'Script"');
// '"Java" + "Script" = "JavaScript"'

也可以看看

¥See also