~
is the bitwise negation operator[MDN]. It converts its operand to a 32-bit integer and swaps all the 1
s to 0
s and all the 0
s to 1
s.
For example:
0000 0000 0000 0000 0000 0000 0000 0000 = 0
1111 1111 1111 1111 1111 1111 1111 1111 = ~0 = -1
Instead of doing text.indexOf(str) !== -1)
you can use the tricky !~text.indexOf(str)
, because ~1 === 0
and !0 === true
.