shorthand for conditions like (x === y) || (x === z)?

Viewed 300

I want to make my condition easier to read and wandering if it is possible to simplify this Scenario:

if((x === y)||(x === z)){...}
//and this one: 
x = (x === y) || (x === z)? ... : ...; 

//To something like: 
if(x === (y || z)){...}
x = x === (y || z) ?  ... : ...; 

This would get rid of the tons of brackets and variable duplication as well as "sounding" logical (at least for me): if this var x is this or that then I do this.

Especially when x is some long object reference e.g.:

this.someObject.KeyOfKeys[200].language === this.someOtherObject.KeyY.languages[10] || ...; 

Or how do you deal with logical "easy" but cumbersome to write conditions?

1 Answers

You can use includes() like so:

if ([y, z].includes(x)) { ... }
x = [y, z].includes(x) ? ... : ...;

As noted in the comments, this will not work with IE11 and older browsers.

Related