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,
- How to get UTC (GMT) time using JavaScript
- Submit html form on dropdown menu value selection or change using javascript
- How to detect Browser and Operating System Name and Version using JavaScript
- JavaScript : Get current page address
- [javaScript] Convert text case to lowercase
- How to check if a String contains substring or a word using javaScript
- Writing your first Hello, World! 🌍 JavaScript code Tutorial
- JavaScript: Check if variable is a number
- How to send email from JavaScript HTML using mailto
- Meaning of javascript:void(0) explained with example
- How to get query string in JavaScript HTML location.search
- Javascript convert text case from uppercase to lowercase
- Loading previous page using html button using JavaScript
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP
- How to get current URL Location using Javascript HTML
- Detect if Cookies are enabled using JavaScript
- Write javaScript code in Swedish using FikaScript
- Add Animated Scrolling to Html Page Title Script
- JavaScript: Convert an Image into Base64 String
- Remove items from JavaScript array
- Send Extra Data with Ajax Get or Post Request
- How to Print from JavaScript HTML using window.print
- Get Device Screen Width and Height using javaScript
- Examples: Convert String to int in JavaScript
- npm WARN saveError ENOENT: no such file or directory, open /mnt/c/package.json
More Posts:
- How to Subscribe to Google Nest Aware on iPhone - Google
- [Solved] Notepad++ Menu Bar Missing - NotepadPlusPlus
- Tomcat Manager Default Username and Password - Tomcat
- How to turn off autocomplete in input fields in HTML Form - Html
- Android SecurityException: Need BLUETOOTH ADMIN permissicacheNameAndAddresson: Neither user 10123 nor current process has android.permission.BLUETOOTH_ADMIN - Android
- How to refresh Safari on Mac (macOS) using keyboard shortcut - MacOS
- Program 8: Multiply Two Numbers - 1000+ Python Programs - Python-Programs
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. - Android