Saw this example on MDN:
According to the documentation,
Bitwise NOTing any number x yields -(x + 1). For example, ~5 yields -6.
So if a character exists within a string,
indexOf will return the first occurance of that character's index which will either be an 0 or any positive nubmer.
Thus, the tilde operator will turn them into:
Any none zero number is considered a
true in JS, so:
str.indexOf() returns any number other than
~str.indexOf() equals to a negative number, which will be considered
In the case of
-1 is returns (i.e. the character does not exist in the string):
else branch is executed.