# logical operator

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [statement](https://lochiwei.gitbook.io/web/js/grammar/statement) ⟩ [expression](https://lochiwei.gitbook.io/web/js/grammar/statement/expr) ⟩ [operator](https://lochiwei.gitbook.io/web/js/grammar/op) ⟩ 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**](https://lochiwei.gitbook.io/web/js/val/prim/nullish), 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](https://lochiwei.gitbook.io/web/js/grammar/op/table-of-operators "mention")
{% endhint %}

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

* <mark style="color:purple;">**`&&`**</mark> and <mark style="color:purple;">**`||`**</mark> can do [short-circuiting](https://lochiwei.gitbook.io/web/js/grammar/op/term/short-circuiting "mention").
* 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**](https://lochiwei.gitbook.io/web/js/val/type/convert) any [val](https://lochiwei.gitbook.io/web/js/val "mention") to [boolean](https://lochiwei.gitbook.io/web/js/val/prim/boolean "mention").

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

{% endhint %}
{% endtab %}

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

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

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

```javascript
// Some code
```

{% endtab %}

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

* [ ] [javascript-the-definitive-guide](https://lochiwei.gitbook.io/web/master/ref/javascript-the-definitive-guide "mention") ⟩ 4.10 Logical Expressions
  {% endtab %}

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/grammar/op/logical.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
