I was very surprised to see the following behavior in Node 14.18.0:
> {}?.fun();
Uncaught TypeError: b?.fun is not a function
> undefined?.fun();
undefined
I understand why the first statement throws a TypeError. {}?.fun
is undefined
, which is not callable. But undefined?.fun
is also undefined
, so why can it be called without throwing? What part of the ECMAScript specification defines this behavior? Did the ECMAScript working group provide any reasons why it should work this way?
During optional chaining, if the current value within the chain is null
or undefined
, the expression short-circuits with a return value of undefined
.
This is in the official docs:
The
?.
operator is like the.
chaining operator, except that instead of causing an error if a reference isnullish
(null
orundefined
), the expression short-circuits with a return value ofundefined
. When used with function calls, it returnsundefined
if the given function does not exist.
Since {}
is an actual object—without a method called fun()
—it blows up, because you called a function that did not exist.
To fix your issue, you will need to call the function with optional chaining:
console.log(({})?.fun?.()); // undefined