JavaScript Shorthands


Example

We can use ~ as a shorthand in some everyday scenarios.

We know that ~ converts -1 to 0, so we can use it with indexOf on array.

indexOf

let items = ['foo', 'bar', 'baz'];
let el = 'a';
if (items.indexOf('a') !== -1) {}

or

if (items.indexOf('a') >= 0) {}

can be re-written as

if (~items.indexOf('a')) {}