tc39 / proposal-optional-chaining

https://tc39.github.io/proposal-optional-chaining/
4.94k stars 75 forks source link

Relationship between optional chaining and null coalescing operator #30

Closed samuelgoto closed 5 years ago

samuelgoto commented 7 years ago

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?

claudepache commented 7 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).

krnlde commented 5 years ago

In short the nullish coalescing part is not the ?. but ??. But I see its a bit misleading in the Syntax section. Issue closed?

dannyskoog commented 5 years ago

Worth mentioning is that both operators aren’t only checking for null, but rather null or undefined