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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
---
title: 条件运算符
slug: Web/JavaScript/Reference/Operators/Conditional_Operator
tags:
- JavaScript
- Operator
translation_of: Web/JavaScript/Reference/Operators/Conditional_Operator
---
<div>{{jsSidebar("Operators")}}</div>
<p><strong>条件(三元)运算符</strong>是 JavaScript 仅有的使用三个操作数的运算符。一个条件后面会跟一个问号(?),如果条件为 {{Glossary("truthy")}} ,则问号后面的表达式A将会执行;表达式A后面跟着一个冒号(:),如果条件为 {{Glossary("falsy")}} ,则冒号后面的表达式B将会执行。本运算符经常作为 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">if</a></code> 语句的简捷形式来使用。</p>
<div>{{EmbedInteractiveExample("pages/js/expressions-conditionaloperators.html")}}</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><em>condition</em> ? <em>exprIfTrue</em> : <em>exprIfFalse</em></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> 的计算结果是 {{Glossary("truthy")}}(它和 <code>true</code> 相等或者可以转换成 <code>true</code> ),那么表达式 <code><var>exprIfTrue</var></code> 将会被求值。</dd>
<dt><code><var>exprIfFalse</var></code></dt>
<dd>如果表达式 <code><var>condition</var></code> 的计算结果是 {{Glossary("falsy")}}(它可以转换成 <code>false</code> ),那么表达式 <code><var>exprIfFalse</var></code> 将会被执行。</dd>
</dl>
<h2 id="描述">描述</h2>
<p>除了 <code>false</code>,可能的假值表达式还有:<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>
<div class="note">
<p><strong>Note:</strong> <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining">The optional chaining operator</a> 设计用来处理这种使用场景。在本文档写成的时候 (2019.01),这个运算符还处于实验阶段并且没有实现。</p>
</div>
<h3 id="条件链">条件链</h3>
<p>这个三元操作符是右结合的,也就是说你可以像这样把它链接起来, 和 <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>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-conditional-operator', 'Conditional Operator')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-conditional-operator', 'Conditional Operator')}}</td>
<td>{{Spec2('ES6')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-11.12', 'The conditional operator')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES1', '#sec-11.12', 'The conditional operator')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition. Implemented in JavaScript 1.0.</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/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>
|