![Best home firewall](https://cdn2.cdnme.se/5447227/9-3/screenshot_1_64e629469606ee7f889a24a7.jpg)
![contains string javascript contains string javascript](https://appdividend.com/wp-content/uploads/2019/06/Javascript-String-Tutorial-With-Example-Strings-in-Javascript.png)
If the word found at the very first of the string, then the position of this value in the string will be 0. The returned value will start from 0 if the value exists in the string. If the word we are looking for exists in the string, then it will return a value that will be must greater than -1. In this tutorial, I have used this method to check if a specific value exists in the string or not. The JavaScript indexOf() method returns the position of specified value in a string. In the above code, I have used the indexOf() method. Now let’s see the JavaScript code below that will check if the string contains a specific word or not: var str = "This is the string where we will check if our word exists." Ĭonsole.log("The specific word doesn't exist") The message will show depending upon if the word exists in the string or not in JavaScript. I am going to print a message on the web page. Here I am going to show you the simple and easy JavaScript code snippets that will do this task. Let's try it out.Today, in this tutorial I am going to show you how to check if a string contains a specific word or not. RegExp.prototype has a test method which returns a boolean. However, that does point us toward something else useful. Unfortunately, with the exception of matching on a regular expression rather than a string, the behavior is identical to indexOf. Looking through the documentation for String.prototype, the search method looks promising due to its name. Ideally, what we're looking for is a method with a name that matches our intention (determining if x contains y), and returns a simple true or false. That means that we can use it, but the clarity of the code suffers. In the event that no match is found, it will return -1. Its job is to return the index at which a given substring is found. While indexOf is often recommended as a simple way to test for the presence of a substring, that's not really its purpose.
![contains string javascript contains string javascript](https://i.ytimg.com/vi/SZKkK908KSw/maxresdefault.jpg)
Var philosophers = "Aquinas, Maimonedes, and Avicenna" var me = "Joshua" function printPhilosopherStatus ( person ) // Outputs: "Joshua is NOT a philosopher." printPhilosopherStatus ( me )
![Best home firewall](https://cdn2.cdnme.se/5447227/9-3/screenshot_1_64e629469606ee7f889a24a7.jpg)