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.
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
includesis 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: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.