javascript function inArray

You could just make an array prototype function ala:

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

if (['test'].hasValue('test')) alert('Yay!');

Note the use of ‘===’ instead of ‘==’ you could change that if you need less specific matching… Otherwise [3].hasValue(‘3’) will return false.

Leave a Comment