# short-circuiting

[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) ⟩ [term](https://lochiwei.gitbook.io/web/js/grammar/op/term) ⟩ short-circuiting

{% hint style="success" %}

```javascript
a && b               // b may not be evaluated
a ?? b               // (same)
condition ? a : b    // only one of a and b is evaluated
obj ?. prop          // prop may not be evaluated
f ?. (args)          // args expressions may not be evaluated
```

:star2: [table-of-operators](https://lochiwei.gitbook.io/web/js/grammar/op/table-of-operators "mention")
{% endhint %}

{% tabs %}
{% tab title="🔴 主題" %}

* [logical](https://lochiwei.gitbook.io/web/js/grammar/op/logical "mention") (&&, ||) - a && b, a || b
* [nullish-coalescing](https://lochiwei.gitbook.io/web/js/grammar/op/logical/nullish-coalescing "mention") - a ?? b
* [conditional-operator](https://lochiwei.gitbook.io/web/js/grammar/op/ternary/conditional-operator "mention") - condition ? a : b
* [optional-chaining](https://lochiwei.gitbook.io/web/js/val/obj/prop/access/optional-chaining "mention") - obj ?. prop, obj ?. \[prop]
* [optional-invocation-.](https://lochiwei.gitbook.io/web/js/val/obj/prop/access/optional-invocation-. "mention") - f ?. (args)
  {% endtab %}

{% tab title="💈範例" %}
replit：[short-circuiting (optional invocation)](https://replit.com/@pegasusroe/short-circuiting-fargs#index.js)

```javascript
let f = null;      // ⭐ not a function
let x = 0;

try { 
    // 1. `x++` evaluated first. ⭐ 
    // 2. TypeError: f is not a function ⛔ 
    f(x++);     
} catch(e) { 
    log(e.name, e.message);
    log(x);        // 1 (x is incremented)
} 

// ⭐ short-circuiting (`x++` not evaluated)
f ?. (x++),        // undefined (no error)
x,                 // 1 (x not incremented)
```

{% endtab %}

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

* [Operator precedence](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence) ⟩ [short-circuiting](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#short-circuiting)&#x20;
  {% endtab %}

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

* [ ] [javascript-the-definitive-guide](https://lochiwei.gitbook.io/web/master/ref/javascript-the-definitive-guide "mention") ⟩ 4.5.1 Conditional Invocation
  {% 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/term/short-circuiting.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.
