Home
About
Resume
Projects
Links
Blog
Back to Contents
# JavaScript Optional Chaining The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called is undefined or null, it returns undefined instead of throwing an error. #### First Seen ```javascript if (HTMLScriptElement.supports?.("importmap")) { console.log("Browser supports import maps."); } ``` from [Mozilla JavaScript Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) #### JavaScript Demo: Expressions - Optional chaining operator ```javascript const adventurer = { name: 'Alice', cat: { name: 'Dinah' } }; const dogName = adventurer.dog?.name; console.log(dogName); // expected output: undefined console.log(adventurer.someNonExistentMethod?.()); // expected output: undefined ``` #### Syntax ```javascript obj.val?.prop obj.val?.[expr] obj.func?.(args) ``` #### Source **Reference:** [Mozilla JavaScript Optional chaining operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)
Previous Post:
JavaScript Generator
Next Post:
Classic Context Menu on Windows 11
Loading