Answers for "function search in javascript array"

4

how to get element of an array in javascript

var value = [a,b,c,d];

var value_b = value[1];
Posted by: Guest on December-02-2020
3

function search in javascript array

// function to search array using for loop
function findInArray(ar, val) {
    for (var i = 0,len = ar.length; i < len; i++) {
        if ( ar[i] === val ) { // strict equality test
            return i;
        }
    }
    return -1;
}

// example array
var ar = ['Rudi', 'Morie', 'Halo', 'Miki', 'Mittens', 'Pumpkin'];
// test the function 
alert( findInArray(ar, 'Rudi') ); // 0 (found at first element)
alert( findInArray(ar, 'Coco') ); // -1 (not found)
Posted by: Guest on December-02-2020

Code answers related to "function search in javascript array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language