Skip to content

Instantly share code, notes, and snippets.

@schwanksta
Created March 31, 2010 23:56
Show Gist options
  • Save schwanksta/351116 to your computer and use it in GitHub Desktop.
Save schwanksta/351116 to your computer and use it in GitHub Desktop.
Javascript doesn't have an "in" operator, so you can use this in_array function instead.
function in_array(val, arr, sep){
/* Takes an array, joins it using sep (default = '|'), then
* uses indexOf to test if val is in arr. Returns true or false.
*/
if(!sep){
var sep = "|";
}
var join = ["", arr.join(sep), ""].join(sep);
var retc = join.indexOf(val);
return retc != -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment