Javascript Array contains(val)

Description

Javascript Array contains(val)


#!/usr/bin/js// ww w.j  av a  2  s . c  o  m

arr = [1, 2, 3, 4, 5];

Array.prototype.contains = function (val) {
  for (var i = 0; i < this.length; i++){
    if (this[i] === val){
      return true;
    }
  }
}

console.log(arr.contains(3))

Javascript Array contains(val)

Array.prototype.contains = function(val) {
  return this.indexOf(val) >= 0;
};



PreviousNext

Related