# JS Tip of the Day: Optional Chaining

**URL:** https://forum.kirupa.com/t/js-tip-of-the-day-optional-chaining/643182
**Category:** web dev
**Created:** [February 14, 2020, 10:10pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-optional-chaining/643182 "2020-02-14T22:10:20Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [February 14, 2020, 10:10pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-optional-chaining/643182/1 "2020-02-14T22:10:20Z")

</div>

**Optional Chaining**  
Version: ES2020  
Level: Intermediate

_To start off the tip of the day list, we’ll start with a brand new feature of JavaScript that only just recently got approved for the JavaScript (ECMAScript) standard: optional chaining!_

Optional chaining is a brand new JavaScript feature introduced in ES2020 that lets you safely dig down into an object reference without having to worry about whether or not any objects within the path are not available or undefined.

Normally, if you attempt to access a property of an object that doesn’t exist, you’ll get an error.

```javascript
let empty = {};
let x = empty.something.x;
// TypeError: Cannot read property 'x' of undefined

```

Here, because `something` is undefined on `empty`, a type error occurs when accessing `x`. The optional chaining operator (`?.`) allows you to attempt the same object reference but will return `undefined` rather than throwing an error.

```auto
let empty = {};
let x = empty?.something?.x;
console.log(x); // undefined

```

This makes it easier to look for a deeply nested value without having to check each object reference along the way or wrap your code in a `try...catch` block. But be careful, this approach doesn’t differentiate between there not being a variable (or object along the way) and the variable existing but having a value of `undefined`.

Optional chaining works in other situations as well, including, but not limited to, function calls.

More info:

- [Optional Chaining on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining)
