Unnecessary use of Boolean literals in conditional expression
The linter is complaining about this:
return found ? true : false
Which should be read as "If found
is truthy return true
otherwise return false
". This structure is referred to as a 'ternary' operator and has been in use since the early days of C, if not before. The ?
operator evaluates the condition on the left and returns the first argument if the condition evaluates to true, otherwise it returns the second argument, where the arguments are separated by a colon.
The problem with your code is that returning the condition itself is the equivalent of returning the boolean literals true
or false
. Therefore, the check and the literals are unnecessary and can be removed. Though, because this is javascript you might want to double negate the condition before returning it, to force it to be a boolean. So, the result looks like this:
return !!found
This is easier to read and there is less chance of it being implemented wrong or misunderstood in the future.
Of course, this could be taken further:
return !!value.find(val => val === item)
In this way, you don't need to even introduce the symbol found
into the code at all. Also, this would be better with some()
, but I think your question is more about the ternary operator than how to search a list.