--- title: 條件運算子 slug: Web/JavaScript/Reference/Operators/Conditional_Operator translation_of: Web/JavaScript/Reference/Operators/Conditional_Operator --- <div>{{jsSidebar("Operators")}}</div> <p><strong>條件 (三元) 運算子</strong> 是 JavaScript 唯一用到三個運算元的運算子:在一個條件後面會跟著一個問號 (<code>?</code>),如果條件是 <a href="/en-US/docs/Glossary/truthy">truthy</a>,在冒號(<code>:</code>)前的表達式會被執行,如果條件是 <a href="/en-US/docs/Glossary/falsy">falsy</a>,在冒號後面的表達式會被執行,這個運算子常常被用來當作 <a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else"><code>if</code></a> 的簡潔寫法.</p> <div>{{EmbedInteractiveExample("pages/js/expressions-conditionaloperators.html")}}</div> <h2 id="語法">語法</h2> <pre class="syntaxbox"><var>condition</var> ? <var>exprIfTrue</var> : <var>exprIfFalse</var></pre> <h3 id="參數">參數</h3> <dl> <dt><code><var>condition</var></code></dt> <dd>值用來做為條件的表達式</dd> <dt><code><var>exprIfTrue</var></code></dt> <dd>如果 <code><var>condition</var></code> 的值是 <a href="/en-US/docs/Glossary/truthy">truthy</a> (等於或是可轉換為 <code>true</code>) , <code><var>exprIfTrue</var></code> 會被執行</dd> <dt><code><var>exprIfFalse</var></code></dt> <dd>如果 <code><var>condition</var></code> 的值是 <a href="/en-US/docs/Glossary/falsy">falsy</a> (等於或是可轉換為 <code>false</code>) , <code><var>exprIfFalse</var></code> 會被執行</dd> </dl> <h2 id="描述">描述</h2> <p>除了 <code>false</code>, 可能是 falsy 的表達式有 <code>null</code>, <code>NaN</code>, <code>0</code>, 空字串 (<code>""</code>) 和 <code>undefined</code>. 如果<code><var>condition</var></code> 是他們其中之一 , 那麼條件表達式的結果會是 <code>exprIfFalse</code> 的執行結果. </p> <p>一個簡單的範例:</p> <pre class="brush: js">var age = 26; var beverage = (age >= 21) ? "Beer" : "Juice"; console.log(beverage); // "Beer" </pre> <p>一個常用來處理 <code>null</code> 的用法 : </p> <pre class="brush: js">function greeting(person) { var name = person ? person.name : "stranger"; return "Howdy, " + name; } console.log(greeting({name: 'Alice'})); // "Howdy, Alice" console.log(greeting(null)); // "Howdy, stranger" </pre> <h3 id="條件鏈">條件鏈</h3> <p>條件 (三元) 運算子是右相依性的 (right-associative), 代表他可以以下面的方式鏈結 , 類似於 <code>if … else if … else if … else</code> 的鏈結方法 :</p> <pre class="brush: js">function example(…) { return condition1 ? value1 : condition2 ? value2 : condition3 ? value3 : value4; } // Equivalent to: function example(…) { if (condition1) { return value1; } else if (condition2) { return value2; } else if (condition3) { return value3; } else { return value4; } } </pre> <h2 id="規範">規範</h2> <table class="standard-table"> <thead> <tr> <th scope="col">Specification</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('ESDraft', '#sec-conditional-operator', 'Conditional Operator')}}</td> </tr> </tbody> </table> <h2 id="瀏覽器相容性">瀏覽器相容性</h2> <p>{{Compat("javascript.operators.conditional")}}</p> <h2 id="參見">參見</h2> <ul> <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if statement</a></li> <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator">Nullish coalescing operator</a></li> <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining">Optional chaining</a></li> <li><a href="/en-US/docs/Learn/JavaScript/Building_blocks/conditionals">Making decisions in your code — conditionals</a></li> <li><a href="/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators">Expressions and operators</a></li> </ul>