Optional chaining enables safe access of properties of an object. It can be enabled in a Node app by using Babel preset-env.
Instructor: [0:00] In a Node app, we can access Object Properties using dot notation in JavaScript. However, if a property's parent doesn't exist, JavaScript will throw an error. To safeguard ourselves in case of accessing members of non-existent properties, we can use Optional Chaining.
[0:21] Optional Chaining is stage 4 ECMAScript proposal. In a Node app, we can use Optional Chaining by using Babel. Babel's environment preset allows us to use latest JavaScript features in Node.
[0:35] We'll first install @babel/preset-env core Node in CLI modules. Then, we will enable Babel by letting it compile our JavaScript, and point our npm start script to run the compiled JavaScript. We'll also enable a preset right here.
[0:57] In our Node app, let's access a property that doesn't exist by using optional chaining. As we can see, the result is undefined, which means we are set to use optional chaining in Node app. To ensure that Babel is working correctly, we can also access a property that exists, and the result is 7, as we expected.