Skip to content

Instantly share code, notes, and snippets.

@donavon
Created July 5, 2011 23:40
Show Gist options
  • Save donavon/1066223 to your computer and use it in GitHub Desktop.
Save donavon/1066223 to your computer and use it in GitHub Desktop.
Does a JavaScript array contain a search element
// Array.contains
// compares searchElement to elements of the Array using strict equality
// (the same method used by the ===, or triple-equals, operator) and returns true or false
// Requires Array.indexOf. If not, see:
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
// ex: [1,2,3].contains(2) returns true
if (!Array.prototype.contains) {
Array.prototype.contains = function (searchElement) {
return this.indexOf(searchElement) !== -1;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment