> 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 %}
