Your Ad Here

Posted By

jlvallelonga on 02/17/09


Tagged

javascript array Value find in


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

brent-man
sheedy
guido


is value in array


 / Published in: JavaScript
 

this function tells you whether or not the passed value is in the passed array

  1. function isValueInArray(arr, val) {
  2. inArray = false;
  3. for (i = 0; i < arr.length; i++)
  4. if (val == arr[i])
  5. inArray = true;
  6. return inArray;
  7. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: sheedy on September 19, 2009

This is exactly what I needed! Thanks!

Posted By: AlexG on March 20, 2011

Function is good, but it can be even better. There is no need to continue loop if element was found. And no boolean variable is needed in the following function:

function isValueInArray(arr, val) { for (i = 0; i < arr.length; i++) if (val == arr[i]) return true; return false; }

You need to login to post a comment.