blob: e2db63da044dea1b22e17e86e28d56c3aaa141ec (
plain)
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
60
61
62
63
64
65
66
67
68
69
|
---
title: 'SyntaxError: missing ) after condition'
slug: Web/JavaScript/Reference/Errors/Missing_parenthesis_after_condition
tags:
- JavaScript
- 语法错误
- 错误
translation_of: Web/JavaScript/Reference/Errors/Missing_parenthesis_after_condition
---
<div>{{jsSidebar("Errors")}}</div>
<h2 id="错误提示">错误提示</h2>
<pre class="syntaxbox">SyntaxError: missing ) after condition
</pre>
<h2 id="错误类型">错误类型</h2>
<p>{{jsxref("SyntaxError")}}</p>
<h2 id="哪里出错了?">哪里出错了?</h2>
<p>这个错误与 if 条件语句是如何写的有关。在任何编程语言中,代码都需要做出一些判断,然后根据不同的判断结果来执行不同的操作。if 语句会在其指定的判断条件为真的时候执行。在 JavaScript 中,条件表达式必须出现在 if 关键字后面的一对括号中,像下面这样:</p>
<pre class="brush: js">if (condition) {
// do something if the condition is true
}</pre>
<h2 id="示例">示例</h2>
<p>下面这种情况的出现可能只是出于大意,需要仔细检查代码中的括号。</p>
<pre class="brush: js example-bad">if (3 > Math.PI {
console.log("wait what?");
}
// SyntaxError: missing ) after condition
</pre>
<p>修复代码的方法就是添加闭合条件表达式的右括号。</p>
<pre class="brush: js example-good">if (3 > Math.PI) {
console.log("wait what?");
}</pre>
<p>如果你是从其他语言转到 JavaScript 的,那么很容易在 JavaScript 中使用与之含义不同或者没有任何意义的关键字。</p>
<pre class="brush: js example-bad">if (done is true) {
console.log("we are done!");
}
// SyntaxError: missing ) after condition
</pre>
<p>相反你需要使用正确的<a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">比较操作符</a>, 如下:</p>
<pre class="brush: js example-good">if (done === true) {
console.log("we are done!");
}</pre>
<h2 id="相关内容">相关内容</h2>
<ul>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if...else</a></code></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators">比较操作符</a></li>
<li>
<p><a href="/en-US/docs/Learn/JavaScript/Building_blocks/conditionals">在代码中做判断 — 条件表达式</a></p>
</li>
</ul>
|