Created
January 18, 2016 10:15
-
-
Save yannbertrand/d40936f1e2af06a3fb96 to your computer and use it in GitHub Desktop.
Simplify the use of indexOf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Array | |
var list = ['my', 'str', 'list']; | |
if (~list.indexOf('my')) { | |
console.log('the `list` array contains \'my\''); | |
} | |
if (!~list.indexOf('number')) { | |
console.log('the `list` array does not contain \'number\''); | |
} | |
// String | |
var hello = 'world'; | |
if (~hello.indexOf('orl')) { | |
console.log('`hello` contains \'orl\''); | |
} | |
if (!~hello.indexOf('qsd')) { | |
console.log('`hello` does not contain \'qsd\''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://github.com/loverajoel/jstips#15---even-simpler-way-of-using-indexof-as-a-contains-clause