1// function to search array using for loop
2function findInArray(ar, val) {
3 for (var i = 0,len = ar.length; i < len; i++) {
4 if ( ar[i] === val ) { // strict equality test
5 return i;
6 }
7 }
8 return -1;
9}
10
11// example array
12var ar = ['Rudi', 'Morie', 'Halo', 'Miki', 'Mittens', 'Pumpkin'];
13// test the function
14alert( findInArray(ar, 'Rudi') ); // 0 (found at first element)
15alert( findInArray(ar, 'Coco') ); // -1 (not found)
16