uTools-Manuals/docs/javascript/Reference/Errors/Missing_parenthesis_after_condition.html

44 lines
2.3 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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