> 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/term/short-circuiting.md).

# short-circuiting

[JS](/web/js.md) ⟩ [statement](/web/js/grammar/statement.md) ⟩ [expression](/web/js/grammar/statement/expr.md) ⟩ [operator](/web/js/grammar/op.md) ⟩ [term](/web/js/grammar/op/term.md) ⟩ 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](/web/js/grammar/op/table-of-operators.md)
{% endhint %}

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

* [logical operator](/web/js/grammar/op/logical.md) (&&, ||) - a && b, a || b
* [nullish coalescing (??)](/web/js/grammar/op/logical/nullish-coalescing.md) - a ?? b
* [conditional operator (?:)](/web/js/grammar/op/ternary/conditional-operator.md) - condition ? a : b
* [optional chaining (?., ?.\[\])](/web/js/val/obj/prop/access/optional-chaining.md) - obj ?. prop, obj ?. \[prop]
* [optional invocation ?.()](/web/js/val/obj/prop/access/optional-invocation-..md) - 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](/web/master/ref/javascript-the-definitive-guide.md) ⟩ 4.5.1 Conditional Invocation
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/grammar/op/term/short-circuiting.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
