语法错误:条件后缺少 )
当 if
条件的编写方式出现错误时,就会出现 JavaScript 异常 "条件后缺少 )"。它必须出现在 if
关键字后面的括号中。
¥The JavaScript exception "missing ) after condition" occurs when there is an error with
how an
if
condition is written. It must appear in parenthesis after the if
keyword.
信息
错误类型
什么地方出了错?
¥What went wrong?
if
条件的写入方式存在错误。在任何编程语言中,代码都需要根据不同的输入做出相应的决策并执行操作。if 语句在指定条件为真时执行语句。在 JavaScript 中,此条件必须出现在 if
关键字后面的括号中,如下所示:
¥There is an error with how an
if
condition is written. In any programming language, code needs to make decisions and
carry out actions accordingly depending on different inputs. The if statement executes a
statement if a specified condition is truthy. In JavaScript, this condition must appear
in parenthesis after the if
keyword, like this:
if (condition) {
// do something if the condition is true
}
示例
缺少括号
¥Missing parenthesis
这可能只是一个疏忽,请仔细检查代码中的所有括号。
¥It might just be an oversight, carefully check all you parenthesis in your code.
if (Math.PI < 3 {
console.log("wait what?");
}
// SyntaxError: missing ) after condition
要修复此代码,你需要添加一个结束条件的括号。
¥To fix this code, you would need to add a parenthesis that closes the condition.
if (Math.PI < 3) {
console.log("wait what?");
}
滥用关键字
¥Misused is keyword
如果你来自其他编程语言,也很容易添加与 JavaScript 含义不同或根本没有含义的关键字。
¥If you are coming from another programming language, it is also easy to add keywords that don't mean the same or have no meaning at all in JavaScript.
if (done is true) {
console.log("we are done!");
}
// SyntaxError: missing ) after condition
相反,你需要使用正确的 比较运算符。例如:
¥Instead you need to use a correct comparison operator. For example:
if (done === true) {
console.log("we are done!");
}
或者甚至更好:
¥Or even better:
if (done) {
console.log("we are done!");
}
也可以看看
¥See also