Javascript Array indexOf() method

Description

Javascript Array indexOf() method


Array.prototype.indexOf = Array.prototype.indexOf || function () {
  var left = 0;/*w  w w . j  a  va2  s .  co m*/
  var right = this.length - 1;
  var middle;
  while (left <= right) {
    middle = Math.floor((left + right) / 2);
    if (this[middle] < arguments[0]) {
      left = middle + 1;
    } else if (this[middle] > arguments[0]) {
      right = middle - 1;
    } else {
      return middle;
    }
  }
  return -1
};



PreviousNext

Related