-
Notifications
You must be signed in to change notification settings - Fork 75
Should null-coalescing operator be included in the proposal? #9
Comments
I'm planning on bringing this up as a stage-0 proposal at the next TC39 meeting. During initial discussions of Null coalescing didn't seem like the right name for something that also checks for undefined, though. |
I like Obviously both proposals would need to agree on "undefined only" or "null or undefined", but that's a stage 2 concern. |
I'll be treating it as a separate proposal and will be seeking stage 1. I'll link the PR to the TC39 agenda in this issue. RE: "null or undefined", I have some strawman arguments for why checking against both is worthwhile. |
Alternative names:
But “null-coalescing” is OK for me (if we choose the “undefined or null” semantics), given that |
The term i use for "undefined or null" is "nullary"; I've not seen a more precise term elsewhere. |
Can you describe this more? Or link to it if it's in the notes? Is this just because |
@jridgewell Here are my rough notes on the syntax. The short version is that ||'s behavior means you can't get optional values out if those values are falsey. Consider: const obj = {
foo: {
bar: {
baz: false // using 'false' for illustrative purposes, also applies to 0, empty strings, and other falsey values.
}
}
}
obj?.foo?.bar?.baz || true // always returns true, will never assign provided value
obj?.foo?.bar?.baz ?? true // returns false, nullary coalescing operator likely matches developer intent |
Once this proposal has a dedicated repository, this issue should be closed. Are there other languages that have something similar to |
C# has ?? and it acts the same as suggested here. |
For prior art, see https://en.wikipedia.org/wiki/Null_coalescing_operator One significant difference is precedence relatively to other operators, see claudepache/es-optional-chaining#10 (comment) |
@ljharb falsy, truthy, nully could be in the same range |
|
@gisenberg do you plan to submit the |
Regarding terminology, lodash uses I have also used the term |
@rattrayalex only tc39 members can submit proposals, ftr. |
Ah, that's right – thanks for the reminder @ljharb . I will amend my comment accordingly. |
@rattrayalex Yes, I intend to submit the proposal. |
I like the idea of pursuing this feature. Either together or separate could be good; the important thing is that they respect the same choice of null/undefined vs just undefined. |
I've added this to the agenda at the next TC39 meetup: tc39/agendas#248 |
Minor changes
a ?? b
evaluates toa
ifa
is not null/undefined,b
otherwise (with same short-circuiting semantics than||
and&&
).Technically, this is completely orthogonal to
?.
. However, it is semantically related.The text was updated successfully, but these errors were encountered: