# 'maybe' operator to complement ?? operator

**URL:** <https://es.discourse.group/t/maybe-operator-to-complement-operator/200>\
**Category:** 💡 Ideas\
**Created:** [January 20, 2020, 9:25pm UTC](https://es.discourse.group/t/maybe-operator-to-complement-operator/200 "2020-01-20T21:25:52Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![dustbort](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/dustbort/32/607_2.png) [@dustbort](https://es.discourse.group/u/dustbort)\
**Post date:** [February 5, 2021, 6:54pm UTC](https://es.discourse.group/t/maybe-operator-to-complement-operator/200/2 "2021-02-05T18:54:47Z")

</div>

We simply need this, although the 'appropriate' syntax is `&?`. Using `??` for nullish coalescing was unfortunate:

In [proposing the nullish coalescing operator](https://github.com/tc39/proposal-nullish-coalescing) `??`, there is not much discussion aside from a reference to C#, from which the `??` syntax was borrowed.  
However, this is shortsighted, since C# uses boolean logic, while ES uses truthy logic. We take advantage of the fact that ES `&&` and `||` operators will return the actual value of the first operand when short-circuiting occurs, rather than `true` and `false`. C# cannot do this, and so is inappropriate to borrow from directly.

In the sense that `&&` mathematically complements `||` in truthy logic, we should have added together the operators `&?` and `|?`. Here, `|?` is the proper name for what we today call `??`, and `&?` is its complement. The usage of `&` and `|` as the first character of the operator is an obvious and natural allusion to the closely related `&&` and `||` operators, and similarly connotes their complementary nature. Indeed, we can regard `&?` and `|?` as narrowing/specializing `&&` and `||`, respectively, so the naming is quite appropriate.

The common names for `&?` and `|?` can be simply "nullish AND" and "nullish OR", respectively. Note that where [some references](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND) define `&&` and `||` as "logical AND" and "logical OR", it should be clear by now that there are [multiple types of "logic" at play (Boolean, nullish, truthy, etc.)](https://dorey.github.io/JavaScript-Equality-Table/) and we should be specific. We love ES's truthy logic and are also now embracing nullish logic!

The need for the `&?` operator comes up in many cases, as you might expect being the proper mathematical complement of `??` (ahem.. `|?`). One example is in using [DataLoader](https://github.com/graphql/dataloader)s in a [GraphQL](https://graphql.org/) resolver, where one needs to guard that the key is not nullish, but allow zero as a key. We cannot use `&&` and we would like to use `&?`.

```javascript
({ key }) => key &? loader.load(key)

```

Without this, we resort to a silly type predicate function in TypeScript:

```javascript
export const ok = <T>(value: T): value is Exclude<T, null | undefined> =>
  // double `not equal` is the operator we need here (`null == undefined`)
  (value != undefined || undefined) as boolean;

```

used as

```javascript
({ key }) => ok(key) && loader.load(key)

```

Note that we had to force-cast the result in TypeScript to be able to use a truthy result where a boolean result is expected.

Unfortunately, we also defined `??=` which carries on the inappropriate C# nomenclature.

---

_[View the full topic](https://es.discourse.group/t/maybe-operator-to-complement-operator/200)._
