JavaScript Tilde Shortcut Indexof Check
Bitwise NOT operator or tilde shortcut:
You can use Array.prototype.indexOf
to check the position of an item in an array. If an item is found it returns the index of the item, if the item is not found it returns -1
.
if([1,2,3,4,5].indexOf(3) !== -1){
console.log('We are here');
}
The bitwise NOT operator will only return 0
for -1
, thus evaluating as falsy inside an if statement:
if(~[1,2,3,4,5].indexOf(3)){
console.log('We are here');
}