JavaScript 使用 ~ 操作符进行 indexOf() 的真值判断 - acwong00/blog GitHub Wiki

var a = "Hello World"

if (a.indexOf('lo') >= 0) { // true }
if (a.indexOf('lo') !== -1) { // true }
if (a.indexOf('lo') < 0) { // false }
if (a.indexOf('lo') === -1) { // false }

这样的写法称为“抽象渗漏”,在代码当中暴露了底层实现,可以用 ~ 代替

var a = "Hello World"

if (~a.indexOf('lo')) { // true }
if (~a.indexOf('ol')) { // false }