In JavaScript, the tilde ~ Bitwise NOT operator is commonly used right before an indexOf() to do a boolean check (truthy/falsy) on a string.

On its own, indexOf() returns the index number of a String object passed in.

var foo = "bar";

foo.indexOf("r"); // 2
foo.indexOf("b"); // 0
foo.indexOf("a"); // 1

Combined with ~, it can do a boolean check if an item exists in a string value:

var foo = "hello world";

if (~foo.indexOf("w")) {
  // item in list
} else {
  // item not in list
}

This works because if a value does not exist the return is -1 for historical reasons. The Bitwise NOT operator effectively works like this:

// ~N -> -(N+1)

So if -1 is returned it will be turned into 0 which is falsy. Anything that is not falsy is truthy.

What to use

Fortunately as of ES7 released in 2016, you can just use Array.includes instead for an accurate boolean check.

var foo = "hello world";

foo.includes("w"); // true
foo.includes("z"); // false

Yay to progress in a language!