/ Published in: JavaScript
this function tells you whether or not the passed value is in the passed array
Expand |
Embed | Plain Text
function isValueInArray(arr, val) { inArray = false; for (i = 0; i < arr.length; i++) if (val == arr[i]) inArray = true; return inArray; }
Comments
Subscribe to comments
You need to login to post a comment.

This is exactly what I needed! Thanks!
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; }