Skip to content

Instantly share code, notes, and snippets.

@Sylvance
Created July 28, 2016 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sylvance/ab02d5a85649697c7874379d7b057f74 to your computer and use it in GitHub Desktop.
Save Sylvance/ab02d5a85649697c7874379d7b057f74 to your computer and use it in GitHub Desktop.
https://repl.it/Cgt4/0 created by Sylvance
var contains = function(needle) {
// Per spec, the way to identify NaN is that it is not equal to itself
var findNaN = needle !== needle;
var indexOf;
if(!findNaN && typeof Array.prototype.indexOf === 'function') {
indexOf = Array.prototype.indexOf;
} else {
indexOf = function(needle) {
var i = -1, index = -1;
for(i = 0; i < this.length; i++) {
var item = this[i];
if((findNaN && item !== item) || item === needle) {
index = i;
break;
}
}
return index;
};
}
return indexOf.call(this, needle) > -1;
};
var myArray = [0,1,2],
needle = 1,
index = contains.call(myArray, needle); // true
console.log(index);
Native Browser JavaScript
true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment