xxxxxxxxxx
const string = "Hello World";
const character = "o";
const index = string.indexOf(character);
if (index !== -1) {
console.log("Character found at index:", index);
} else {
console.log("Character not found in the string.");
}
xxxxxxxxxx
/*To search for the index of a substring or string within an array,
use .indexOf()
If you search for something that isn't there, the function will return -1*/
let mechs = ["madcat", "blood asp", "atlas"];
console.log(mechs.indexOf("madcat"); //returns 0
console.log(mechs.indexOf("atlas"); //returns 2
let clan = "nova cat";
console.log(clan.indexOf("a")); /*returns 3 (Only the first instance of the
character is used.*/
console.log(clan.indexOf("cat")); /*returns 5 (Finds the index of the beginning
character)*/
console.log(clan.indexOf("s")); //returns -1