Enable optional chaining in Node app by using Babel

Shruti Kapoor
InstructorShruti Kapoor
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 2 years ago

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.

egghead
egghead

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today