Proposal: result-forwarding ternary operator

Michael Rosefield rosyatrandom at
Tue Sep 19 12:33:16 UTC 2017

(I've also put this on reddit
which I've copied this from. Hope the formatting doesn't go haywire...)

First course of action for this proposal is, obviously, to
come up with a better name for it....


As with the 'optional chaining' proposal for tc39
<>, this operator is a
way to avoid excess and annoying code from safety-checking.

The optional chaining proposal, above, follows a chain and short-circuits
it upon acting on a null object, returning a safe '*undefined*' result; it
can be thought of as an extended '*if*' sequence. It looks like this:

// safeVal = result of someProp, or undefined if looking for props on null obj
const safeVal = blah?.someMethod()?.someProp;

This proposal provides for an '*else'* scenario, particularly in situations
where chaining isn't appropriate, by forwarding the result of a truthy
conditional check to a single-parameter function.


*condition ?! fn : expr*


   - condition: any condition, *identical to use in standard ternary*
   - fn: function taking single parameter, which is the result of
   evaluating *condition*
   - expr: any expression, *identical to use in standard ternary*

Usage Example

// temporary variable
const temp = getSomething(),
      foo = temp ? doSomething(temp) : doSomethingElse();

// repeated code, possible side-effects
const foo2 = getSomething() ? doSomething(getSomething()) : doSomethingElse();

// proposal, no chaining
const newFoo = getSomething() ?! x => doSomething(x) : doSomethingElse();

// proposal, chaining
const newFoo = getSomething() ?!
      x => {
        return x.doSomething();
      } :



   The choice of '?!' is entirely arbitrary and not a core part of the

   The result of the conditional check is not passed on to the falsey path,
   because it seems pointless to do so.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list