Short Comparing proposal

Felipe Nascimento de Moura felipenmoura at gmail.com
Wed Feb 1 16:27:24 UTC 2017


Hi.

I wonder if there is already a proposal for such thing or if it would be
something interesting to be proposal:

*The motivation:*
Let's say you have an object whose property you want to compare to 'a' or
'b':

```
if (object.property.secondProp === 'a' || object.property.secondProp ===
'b') {
  // do something
}
```

*The proposal:*
Now, with this syntax you would be able to perform such comparisons in a
much simpler, better to read approach:

```
if (object.property.secondProp === ('a' : 'b')) {
    // do Something
}
```

And with the advance of the Optional Chaining Operator
<https://github.com/claudepache/es-optional-chaining> proposal, it would
get even better, like so:

```
// this:
if (object &&
    object.property &&
    (object.property.secondProp === 'a' ||
    object.property.secondProp === 'b')) {
  // ...
}

// becomes this:
if (object?.property?.secondProp === ('a' : 'b')) { ... }
```

*Alternatives:*
I do know that we could accomplish that with other techniques, like:

```
['a', 'b'].includes(object.property.secondProp)
```

I just think it might be counterintuitive and having a syntax for a "short
comparing" would be better for reading and understanding.

Would make ternary comparison even shorter, too:

```
let currentState = object.property.status === ('fail' : 'ok') ? 'done' :
'doing'
```

Please let me hear your thoughts on that :)

Regards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170201/558a38a5/attachment.html>


More information about the es-discuss mailing list