> 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/val/obj/prop/access/optional-invocation-./is-supported.md).

# is Optional Chaining Supported?

{% tabs %}
{% tab title="💾 程式" %}

* [replit](https://replit.com/@pegasusroe/JS-is-optional-chaining-supported#index.js), [codepen](https://codepen.io/lochiwei/pen/dydZGMX?editors=0012)

```javascript
function isOptionalChainingSupported() {
    try {
        
        const obj = {};
        
        let shouldBeUndefinedIfSupported = null;

        // ❗❗❗ JavaScript CANNOT catch a SyntaxError ❗❗❗
        // --------------------------------------------------
        // ⛔ SyntaxError (if not supported)
        // const shouldBeUndefinedIfSupported = obj ?. foo;
        //    Unexpected token '.' -------------> ^

        // ⭐ eval() will cause and throw a SyntaxError if failed❗
        //    BUT won't stop the program❗
        eval(`shouldBeUndefinedIfSupported = obj ?. foo`);

        // jump to "catch" block if failed
        
        // do support
        // log(`program code just passed "eval"❗`);
        return (shouldBeUndefinedIfSupported === undefined);
        
    } catch(e) {
    
        // error has occurred, optional chaining NOT supported.
        if (e instanceof SyntaxError) {
            log(`⛔ SyntaxError: ${e.message}`);
        }
        
        return false
    }
}
```

💈範例：

```javascript
console.log(isOptionalChainingSupported());
```

{% endtab %}

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

* [try...catch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch)
* [SyntaxError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError)
* [eval()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)
  {% endtab %}

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

* [ ] [Check if optional chaining is supported](https://javascript.tutorialink.com/check-if-optional-chaining-is-supported/)
  {% endtab %}

{% tab title="🗣 討論" %}

* [eval javascript, check for syntax error](https://stackoverflow.com/questions/4923316/eval-javascript-check-for-syntax-error)
* [Can syntax errors be caught in JavaScript?](https://stackoverflow.com/questions/5963045/can-syntax-errors-be-caught-in-javascript)
  {% endtab %}
  {% endtabs %}
