> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/grammar/statement/flow/branch/if.md).

# if

🚧 under construction

[JS](/web/js.md) ⟩ [statement](/web/js/grammar/statement.md) ⟩ [control flow](/web/js/grammar/statement/flow.md) ⟩ [branch](/web/js/grammar/statement/flow/branch.md) ⟩ if

{% hint style="success" %}
([statement](/web/js/grammar/statement.md))&#x20;

```javascript
if (condition) statement                     // one-way branch
if (condition) statement1 else statement2    // two-way branch
```

{% endhint %}

{% tabs %}
{% tab title="🧨 雷區" %}
{% hint style="danger" %} <mark style="color:purple;">**else**</mark>**&#x20;**<mark style="color:orange;">**clause**</mark> is <mark style="color:yellow;">**part of**</mark> the <mark style="color:yellow;">**nearest**</mark> <mark style="color:purple;">**if**</mark> statement.

```javascript
if (expr1) statement1    // not this one
    if (expr2) statement2      // ... is part of this if statement
    else statement3            // else clause ...
```

{% endhint %}
{% endtab %}

{% tab title="👥 相關" %}

* [switch](/web/js/grammar/statement/flow/branch/switch.md) - multiway branches.
* [falsy](/web/js/val/type/falsy.md) values.
  {% endtab %}

{% tab title="📗 參考" %}

* [JavaScript: The Definitive Guide](/web/master/ref/javascript-the-definitive-guide.md) ⟩ 5.3.1 if
  {% endtab %}

{% tab title="📘 手冊" %}

* [ ] [Statements and declarations](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements) ⟩ [control flow](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements#control_flow) ⟩ if
  {% endtab %}
  {% endtabs %}
