Skip to content

Instantly share code, notes, and snippets.

@dgoodlad
Created July 6, 2010 05:49
Show Gist options
  • Save dgoodlad/465062 to your computer and use it in GitHub Desktop.
Save dgoodlad/465062 to your computer and use it in GitHub Desktop.
Apparently JS has a tilde (~) operator. It's a unary operator that returns -(N + 1). So, using it and indexOf seems to give you a simple but unreadable test for substring detection. Since indexOf('stringthatisntthere') returns -1, the ~ operator turns that into -(-1 + 1) = -0 = 0, which is false. Otherwise, it'll always be true.
var ua = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.2 (KHTML, like Gecko) Chrome/6.0.453.1 Safari/534.2";
~ua.indexOf("Firefox") // => false
~ua.indexOf("Chrome") // => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment