It's the bitwise NOT operator. It will convert the operand to an 32-bit integer, then yields one's complement (inverts every bit) of that integer.
Finally, !
will return true
if and only only if the result of that operation is 0
.
Some examples might help:
x | x (bin) | ~x (bin) | ~x | !~x
-3 | 1111…1101 | 0000…0010 | 2 | false
-2 | 1111…1110 | 0000…0001 | 1 | false
-1 | 1111…1111 | 0000…0000 | 0 | true
0 | 0000…0000 | 1111…1111 | -1 | false
1 | 0000…0001 | 1111…1110 | -2 | false
In other words,
if ( !~text.indexOf('a') ) { }
is equivalent to:
if ( text.indexOf('a') == -1 ) { }