Javascript Array indexOf(searchElement)

Description

Javascript Array indexOf(searchElement)


// Array.prototype.indexOf
Array.prototype.indexOf = function indexOf(searchElement) {
  for (var array = this, index = 0, length = array.length; index < length; ++index) {
    if (array[index] === searchElement) {
      return index;
    }/*from   ww w .  j  a va 2  s  .  c o  m*/
  }

  return -1;
};

Javascript Array indexOf(searchElement)

Array.prototype.indexOf = function indexOf(searchElement) {
 if (this === undefined || this === null) {
  throw new TypeError(this + ' is not an object');
 }

 var/*w w w.  j a  v  a2s .c o  m*/
 arraylike = this instanceof String ? this.split('') : this,
 length = Math.max(Math.min(arraylike.length, 9007199254740991), 0) || 0,
 index = Number(arguments[1]) || 0;

 index = (index < 0 ? Math.max(length + index, 0) : index) - 1;

 while (++index < length) {
  if (index in arraylike && arraylike[index] === searchElement) {
   return index;
  }
 }

 return -1;
};



PreviousNext

Related