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 contain the word apple!");
} else {
console.log("myString1 does not contain the word apple!")
}
if(val2 > -1) {
console.log("myString2 contain the word apple!");
} else {
console.log("myString2 does not contain the word apple!")
}
Output:
"myString1 contain the word apple!"
"myString2 does not contain the word apple!"

JavaScript contains substring example code
Check out jsfiddle: https://jsfiddle.net/9h3zcd0q/
More Posts related to JavaScript,
- Meaning of javascript:void(0) explained with example
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- How to get UTC (GMT) time using JavaScript
- Add Animated Scrolling to Html Page Title Script
- Examples: Convert String to int in JavaScript
- Get Device Screen Width and Height using javaScript
- Remove items from JavaScript array
- Javascript convert text case from uppercase to lowercase
- [javaScript] Convert text case to lowercase
- How to detect Browser and Operating System Name and Version using JavaScript
- How to send email from JavaScript HTML using mailto
- How to check if a String contains substring or a word using javaScript
- Send Extra Data with Ajax Get or Post Request
- How to get query string in JavaScript HTML location.search
- How to get current URL Location using Javascript HTML
- JavaScript: Check if variable is a number
- JavaScript : Get current page address
- JavaScript: Count Words in a String
- Loading previous page using html button using JavaScript
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
- How to Print from JavaScript HTML using window.print
- Submit html form on dropdown menu value selection or change using javascript
- Detect if Cookies are enabled using JavaScript
- Writing your first Hello, World! 🌍 JavaScript code Tutorial
- JavaScript date in yyyy-MM-dd format
More Posts:
- Align left align text in Bootstrap - Bootstrap
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..] - Java
- Java XML-RPC java.net.BindException: Address already in use - Java
- How to get the Android OS installed version programmatically - Android
- SharePoint Designer 2010 - errors were found when compiling the workflow - SharePoint
- How to uninstall/remove or disable Microsoft Teams - Teams
- How to generate client id and secret to register SharePoint App with OAuth - SharePoint
- Upload docx file using PHP script - PHP