javascript search function code example
Example 1: js string search
var str = "This is a test sentence";
var hasTest = str.includes("test");
if(hasTest == true){
}
Example 2: function search in javascript array
function findInArray(ar, val) {
for (var i = 0,len = ar.length; i < len; i++) {
if ( ar[i] === val ) {
return i;
}
}
return -1;
}
var ar = ['Rudi', 'Morie', 'Halo', 'Miki', 'Mittens', 'Pumpkin'];
alert( findInArray(ar, 'Rudi') );
alert( findInArray(ar, 'Coco') );
Example 3: search in javascript
str.indexOf("locate");
str.lastIndexOf("locate");
str.indexOf("locate", 15);
str.search("locate");