> 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/op/logical.md).

# logical operator

🚧 under construction -> example

[JS](/web/js.md) ⟩ [statement](/web/js/grammar/statement.md) ⟩ [expression](/web/js/grammar/statement/expr.md) ⟩ [operator](/web/js/grammar/op.md) ⟩ logical ⟩&#x20;

{% hint style="success" %}

* <mark style="color:purple;">**`a && b`**</mark> ：<mark style="color:yellow;">**returns**</mark> <mark style="color:purple;">**`a`**</mark> <mark style="color:red;">**if**</mark> <mark style="color:purple;">**`a`**</mark> is <mark style="color:red;">**falsy**</mark>, otherwise <mark style="color:yellow;">**returns**</mark> <mark style="color:purple;">**`b`**</mark>.&#x20;
* <mark style="color:purple;">**`a || b`**</mark>：<mark style="color:yellow;">**returns**</mark> <mark style="color:purple;">**`a`**</mark> <mark style="color:red;">**if**</mark> <mark style="color:purple;">**`a`**</mark> is <mark style="color:green;">**truthy**</mark>, otherwise <mark style="color:yellow;">**returns**</mark> <mark style="color:purple;">**`b`**</mark>.&#x20;
* <mark style="color:purple;">**`a ?? b`**</mark>：<mark style="color:yellow;">**returns**</mark> <mark style="color:purple;">**`a`**</mark> <mark style="color:red;">**if**</mark> <mark style="color:purple;">**`a`**</mark> is [**non-nullish**](/web/js/val/prim/nullish.md), otherwise <mark style="color:yellow;">**returns**</mark> <mark style="color:purple;">**`b`**</mark>.&#x20;
* <mark style="color:purple;">**`!a`**</mark>：<mark style="color:yellow;">**returns**</mark> <mark style="color:red;">**`false`**</mark> <mark style="color:red;">**if**</mark> <mark style="color:purple;">**`a`**</mark> is <mark style="color:green;">**truthy**</mark>, otherwise <mark style="color:yellow;">**returns**</mark> <mark style="color:green;">**`true`**</mark>.&#x20;

:star2: [table of operators](/web/js/grammar/op/table-of-operators.md)
{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="warning" %}

* <mark style="color:purple;">**`&&`**</mark> and <mark style="color:purple;">**`||`**</mark> can do [short-circuiting](/web/js/grammar/op/term/short-circuiting.md).
* unlike <mark style="color:purple;">**`&&`**</mark> and <mark style="color:purple;">**`||`**</mark>, <mark style="color:orange;">**`!`**</mark> <mark style="color:yellow;">**always**</mark> returns <mark style="color:green;">**true**</mark> or <mark style="color:red;">**false**</mark>.
  {% endhint %}

{% hint style="success" %}
[**convert**](/web/js/val/type/convert.md) any [value](/web/js/val.md) to [Boolean](/web/js/val/prim/boolean.md).

```javascript
!!x    // conversion: any -> true/false
```

{% endhint %}
{% endtab %}

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

* [falsy](/web/js/val/type/falsy.md) <mark style="color:yellow;">**/**</mark> [nullish](/web/js/val/prim/nullish.md)
* [conditional operator (?:)](/web/js/grammar/op/ternary/conditional-operator.md)
* [optional chaining (?., ?.\[\])](/web/js/val/obj/prop/access/optional-chaining.md)
* [nullish coalescing (??)](/web/js/grammar/op/logical/nullish-coalescing.md)
  {% endtab %}

{% tab title="💈範例" %}
replit：

```javascript
// Some code
```

{% endtab %}

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

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

{% tab title="📘 手冊" %}
\*
{% endtab %}
{% endtabs %}
