indexOf Examples¶
/*The general syntax for this method is:
stringName.indexOf(substr);*/
console.log("LaunchCode".indexOf("C"));
console.log("LaunchCode".indexOf("A"));
console.log("dogs and dogs and dogs!".indexOf("dog"));
//6
//-1
//0
/*An email address must contain an @ symbol. Checking for the presence
of this symbol is a part of email address verification in most
programs.*/
let input = "[email protected]";
let atIndex = input.indexOf("@");
if (atIndex > -1) {
console.log("Email contains @");
} else {
console.log("Invalid email");
}
//Email contains @