How can I use optional chaining with arrays and functions?

You need to put a . after the ? to use optional chaining: myArray.filter(x => x.testKey === myTestKey)?.[0] Playground link Using just the ? alone makes the compiler think you’re trying to use the conditional operator (and then it throws an error since it doesn’t see a : later) Optional chaining isn’t just a TypeScript … Read more

In JavaScript, is there an easier way to check if a property of a property exists?

I find this very convenient: var myVal = (myVal=appData) && (myVal=myVal.foo) && (myVal=myVal.bar) && myVal.settings; If a property exists, the next part of the sequence will be attempted. When the expression before && evaluates to false, the next part of the expression will not be checked. If either of myVal.appData.foo.bar.settings is not defined, the value … Read more

Optional Chaining in JavaScript [duplicate]

This is currently a Stage 4 proposal you can check on the progress of it here: https://github.com/tc39/proposal-optional-chaining You can use the babel plugin today: https://www.npmjs.com/package/babel-plugin-transform-optional-chaining Update 11th January 2020: Babel now supports optional chaining by default https://babeljs.io/blog/2020/01/11/7.8.0 The Optional Chaining operator is spelled ?.. It may appear in three positions: obj?.prop // optional static property … Read more