Closed samuelgoto closed 5 years ago
For the difference between the two... well, it’s just that they do two different things:
Optional chaining: It is essentially a conditional property access (or function call).
a?.p // `a.p` if `a` is not null
f?.() // invokes `f` if `f` is not null
Null coalescing: Among several expressions, you take the first one that is not-null:
a ?? b // `a` if `a` is not null, `b` otherwise
As for “the nature of the problems each is trying to solve”, I’m not sure what to add to the explainer of each proposal (and https://github.com/gisenberg/proposal-nullary-coalescing/issues/2 for additional motivations not included in the explainer).
In short the nullish coalescing part is not the ?.
but ??
. But I see its a bit misleading in the Syntax section. Issue closed?
Worth mentioning is that both operators aren’t only checking for null
, but rather null
or undefined
I keep reading the null coalescing operator proposal thinking that they are the same and it is really unclear to me what is the relationship between the two.
Can you help me rationalize the nature of the problems each is trying to solve and why we should think about them differently?