Backbencher.dev

JS Daily 19 - Optional Chaining

Last updated on 8 Dec, 2020

What is the output of following code?

const obj = {
  a: false
};

console.log(obj.a?.());
----o----

The output is TypeError. It says TypeError: obj.a is not a function.

Optional Chaining operator checks for only null or undefined on the left hand side. In this case a has the value false. So optional chaining operator continues chaining and it results in calling a function like false(). That results in TypeError.

--- ○ ---
Joby Joseph
Web Architect