diff options
Diffstat (limited to 'files/vi/web/javascript/reference/operators/conditional_operator')
-rw-r--r-- | files/vi/web/javascript/reference/operators/conditional_operator/index.html | 119 |
1 files changed, 0 insertions, 119 deletions
diff --git a/files/vi/web/javascript/reference/operators/conditional_operator/index.html b/files/vi/web/javascript/reference/operators/conditional_operator/index.html deleted file mode 100644 index 62cc4cadb5..0000000000 --- a/files/vi/web/javascript/reference/operators/conditional_operator/index.html +++ /dev/null @@ -1,119 +0,0 @@ ---- -title: Conditional (ternary) operator -slug: Web/JavaScript/Reference/Operators/Conditional_Operator -translation_of: Web/JavaScript/Reference/Operators/Conditional_Operator ---- -<div>{{jsSidebar("Operators")}}</div> - -<p><strong>Toán tử điều kiện (ba ngôi)</strong> là toán tử duy nhất của JavaScript cần tới ba toán hạng. Toán tử này thường dùng tắt cho câu lệnh <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="Cú_pháp">Cú pháp</h2> - -<pre class="syntaxbox"><em>condition</em> ? <em>exprT</em> : <em>exprF</em> </pre> - -<h3 id="Tham_số">Tham số</h3> - -<dl> - <dt><code>condition</code></dt> - <dd>Biểu thức mà giá trị của nó được dùng như điều kiện.</dd> -</dl> - -<dl> - <dt><code>exprT</code>, <code>exprF</code></dt> - <dd>Biểu thức với giá trị có kiểu bất kỳ.</dd> -</dl> - -<h2 id="Mô_tả">Mô tả</h2> - -<p>Nếu <code>condition</code> có thể chuyển đổi thành<code>true</code> (hay là {{Glossary("truthy")}}), toán tử trả về giá trị của <code>exprT</code>; ngược lại (khi <code>condition</code> là {{Glossary("falsy")}}), nó trả về giá trị của <code>exprF</code>.</p> - -<p>(Trong cả hai trường hợp, biểu thức còn lại đều không được duyệt tới.)</p> - -<p>Ngoài <code>false</code> ra, các biểu thức falsy có thể xảy ra bao gồm: <code>null</code>, <code><code>NaN</code></code>, <code><code><code>0</code></code></code>, xâu ký tự rỗng (<code><code><code><code>""</code></code></code></code>), và <code><code><code><code>undefined</code></code></code></code>. Nếu <code>condition</code> là một trong những gì vừa liệt kê phía trên, kết quả của biểu thức điều kiện sẽ là <code>exprF</code>.</p> - -<p>Ví dụ đơn giản:</p> - -<pre class="brush: js">var age = 26; -var beverage = (age >= 21) ? "Beer" : "Juice"; -console.log(beverage); // "Beer" -</pre> - -<p>Thường dùng để xử lý giá trị <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="Điều_kiện_nối_tiếp">Điều kiện nối tiếp</h3> - -<p>Toán tử điều kiện tuân theo suy dẫn phải, tức là nó có thể được gọi "nối tiếp" theo cách sau đây, tương tự như với <code>if … else if … else if … else</code> nối tiếp nhau:</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="Đặc_tả">Đặc tả</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Đặc tả</th> - <th scope="col">Trạng thái</th> - <th scope="col">Bình luận</th> - </tr> - <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>Định nghĩa lần đầu. Cài đặt trong JavaScript 1.0.</td> - </tr> - </tbody> -</table> - -<h2 id="Tương_thích_trình_duyệt">Tương thích trình duyệt</h2> - - - -<p>{{Compat("javascript.operators.conditional")}}</p> - -<h2 id="Xem_thêm">Xem thêm</h2> - -<ul> - <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/if...else">Lệnh if</a></li> -</ul> |