Skip to content

Instantly share code, notes, and snippets.

@omurphy27
Created July 27, 2016 06:14
Show Gist options
  • Save omurphy27/c64ba822bb52a0d6b9d12ed5916e4620 to your computer and use it in GitHub Desktop.
Save omurphy27/c64ba822bb52a0d6b9d12ed5916e4620 to your computer and use it in GitHub Desktop.
JS - Javascript - Finding a match in a JS array with indexOf
// where this.conditionsLetters is
// ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
if ( this.conditionsLetters.indexOf( $letter ) > -1 ) { // check if match found
this.selectedConditionsLetter = $letter;
}
// no match returns -1,
// whereas a match returns the zero base position in the array which is always greater than -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment