const onlyArr = ["1","3","4","5"]
const isExist = onlyArr.indexOf("3") !== -1
console.log('isExist',isExist)
Output=> true
const onlyArr = ["1","3","4","5"]
const isExist = onlyArr.indexOf("3") !== -1
console.log('isExist',isExist)
Output=> true
For further actions, you may consider blocking this person and/or reporting abuse
VAIBHAV RAI -
Preethi Puttegowda -
Tutort Academy -
Mike Young -
Top comments (4)
You can shorten this to:
Or, using something much more readable:
The first time I saw the
~
(Bitwise NOT) operator being usedI tried it and it gives a negative number if the element is present and
0
if it is not.So why not use
!!
in front of it which will give the answer in a booleanRip readability tho... but yeah the second option is preferable using
.includes()
and the one which OP mentioned, I was just messing around with the~
operatorA boolean is unnecessary since values have truthiness and falsiness -
-1
will become a falsy value (0), everything else will be truthy (negative numbers). Converting manually to a Boolean is inefficient and unnecessaryOh yeah that's true, didn't think of that way.
Thank you!