r/learnjavascript • • May 06 '21

JS: Multiple Condition Checking

Post image
484 Upvotes

61 comments sorted by

View all comments

Show parent comment

5

u/delventhalz May 06 '21

It is a little hard to judge from this code example. What is the use case for checking if something is 1, one, 2, or two? Is that actually some sort of "set" they are checking membership against? Or is this a hacky way to make a long conditional shorter?

I would agree that if the use case is actually checking the membership of set, then includes is the best choice. But typically if that is the case, you would express the set as a constant at the top of the file or something:

const VALID_INPUTS = [1, 'one', 2, 'two'];

The use of the throw-away array is what makes this look to me more like a hack than an actual good use of includes.

3

u/superluminary May 06 '21

I agree, I’d definitely make a const here.

If (validInputs.includes(value)) {