in a node repl or browser console:
> ({})?.a
undefined
> (null)?.a
undefined
> (null)?.a.b
undefined
> ({})?.a.b
Uncaught TypeError: Cannot read properties of undefined (reading 'b')
if (anything)?.a is undefined then why does the undefined that (null)?.a evaluates to NOT throw an error when I read its non-existent properties?
({})is notundefined, but there's no "a" property in that object so({})?.ais undefined, just like({a: 1}).bisundefined.undefined.?isnullorundefined. Since{}is neither of those, evaluation continues without short circuiting and throws the error for the same reason({}}.a.bwould throw attempting to look up thebproperty ofundefined.(null)?.a.band(null?.a).b. The one NickParsons dug up is more suited as a duplicate, though all the info there is in the comments not the answer.