If the situation is such that b
, if it exists (isn't undefined or null), will be an object, then no, there isn't any difference between those two.
The largest reason why you probably see someVar && someVar.someProp
(or !!someVar && someVar.someProp
) and variations is that optional chaining is pretty new syntax. It will only exist in recently-updated codebases (running TypeScript 3.7 or above).
But if a variable may be falsy, but is not necessarily an object - for example, if it's 0, NaN, false, or the empty string, then those constructs are not equivalent. Optional chaining with ?.
will short-circuit to undefined
only if expression to the left of the ?.
is null
or undefined
. Other falsy values will continue to have their properties be evaluated as normal.