In order to know if a particular String contains a given word or a substring you can make use of the indexOf() method in JavaScript
String.prototype.indexOf(): The indexOf method will return the index of the calling String object, it will return the first occurrence of the provided substring value. It will return -1 if the provided substring is not found.
Example:<h2>JavaScript Substring example:</h2>
<div id="myString1">This is some text and i want to find if the work
apple is in this string or not!</div>
<br/>
<div id="myString2">This string does not contains that word!</div>
var val1 = document.getElementById("myString1").innerHTML.indexOf("apple");
var val2 = document.getElementById("myString2").innerHTML.indexOf("apple");
if(val1 > -1) {
console.log("myString1 contains the word apple!");
} else {
console.log("myString1 does not contain the word apple!")
}
if(val2 > -1) {
console.log("myString2 contains the word apple!");
} else {
console.log("myString2 does not contain the word apple!")
}
Output:
"myString1 contains the word apple!"
"myString2 does not contain the word apple!"

JavaScript contains substring example code
More Posts related to JavaScript,
- Get Current time in GMT/UTC using JavaScript
- How to get current URL Location using Javascript HTML
- Excel Fix: SECURITY RISK Microsoft has blocked macros from running because the source of this file is untrusted.
- Convert Javascript object to JSON String example
- Fix: ReferenceError: require is not defined in ES module scope [Node]
- [JavaScript] Remove all Newlines From String
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- Fix: Error: error:0308010C:digital envelope routines::unsupported NodeJs/Vue/React
- Pure JavaScript Digital Clock Widget to get GMT/UTC Time Now
- Fix: npm vs code eacces permission denied unlink /usr/local/bin/code
- Power of Print Statements in JavaScript: A Comprehensive Guide
- [javaScript] Convert text case to lowercase
- JavaScript: Count Words in a String
- JavaScript date in yyyy-MM-dd format
- How to send email from JavaScript HTML using mailto
- How to get UTC (GMT) time using JavaScript
- JavaScript: Generate Random Numbers between 1 and 3
- How to get query string in JavaScript HTML location.search
- Fix: Uncaught ReferenceError: exit is not defined - Node.js REPL
- Detect if Cookies are enabled using JavaScript
- Submit html form on dropdown menu value selection or change using javascript
- Send Extra Data with Ajax Get or Post Request
- Loading previous page using html button using JavaScript
- 10 ways to Convert String to a Number in JavaScript
- JavaScript: Check if variable is a number
More Posts:
- How to Mount Google Drive in Colab Notebook - Google
- How to Hardcode Date in Java with Examples - Java
- What version of windows do I have? - Microsoft
- iOS 14 Airpods Connected message everytime when the iPhone is unlocked - Apple
- How to install Java on macOS [Big Sur] - MacOS
- How to Install Compare Two Files Plugin in Notepad++ - NotepadPlusPlus
- How to install fps unlocker for Roblox on Mac - MacOS
- How to place two div elements next to each other - CSS