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.
let items = ['foo', 'bar', 'baz'];
let el = 'a';
if (items.indexOf('a') !== -1) {}
or
if (items.indexOf('a') >= 0) {}
if (~items.indexOf('a')) {}