let string = "This is a string"; /** * Now we can check if * string contains substring * using indexOf() * */ if (string.indexOf("a") !== -1) { console.log("There is a substring"); } else { console.log("Substring not found"); } //Result: // "There is a substring" /** * Let's use includes() for * the same action * */ if (string.includes("a")) { console.log("There is a substring"); } else { console.log("Substring not found"); } //Result: // "There is a substring" /** * Let's check if index is larger * or smaller than number * */ if (string.indexOf("a") > 10) { console.log("A substring is too far"); } else { console.log("A substring is close enough"); } //Result: // "A substring is close enough"