search string in string javascript code example

Example 1: search in string array javascript

function searchStringInArray (str, strArray) {
    for (var j=0; j<strArray.length; j++) {
        if (strArray[j].match(str)) return j;
    }
    return -1;
}

Example 2: check for substring javascript

const string = "javascript";
const substring = "script";

console.log(string.includes(substring));  //true

Example 3: find in string javascript

stringToBeSearched.search(stringToBeFound);

Example 4: js string search

var str = "This is a test sentence";
var hasTest = str.includes("test");

if(hasTest == true){
	//do a thing 
}

Example 5: search inside a string javascript

var string = "Hello world!";
var n = string.search("w"); //now n equals 6