So, I just want to ask, is it some more beautify way to write such conditions:
state === 'su' || state === 'ja' || state === 'fa'
I mean, maybe they can be rewrited like:
state === ('su' || 'ja' || 'fa')
Thanks
So, I just want to ask, is it some more beautify way to write such conditions:
state === 'su' || state === 'ja' || state === 'fa'
I mean, maybe they can be rewrited like:
state === ('su' || 'ja' || 'fa')
Thanks
As @Paulpro stated in his comment. You can use
['su','ja','fa'].includes( state )
.
I am adding it as an answer, so It'd be useful for someone sometime and easy to find.
As said by @Paulpro - you can really easy check a lot of incoming value by array method includes
. So, we can make it in more beauty way as you want.
see for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
You can use the indexOf() method that arrays have, e.g.
['su', 'ja', 'fa'].indexOf('ja') > -1; // true
For maximum beauty, have your aray declared as a variable first, e.g.
valid = ['su', 'ja', 'fa', 'la', 'ti', 'do'];
valid.indexOf(state) > -1;
This way, your code looks tidy even if the list of valid values is long.
See more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
I think that you should write (state === 'su' || state === 'ja' || state === 'fa')
.
It's simpler that way for other programmer to understand.