To search for a string inside another string, you can use the
Let's say you have a string called
Hello 😃, How are you?
// a string const str = "Hello 😃, How are you?";
And now let's check whether the above string contains the word
How. So let's use the
includes() method on the
str to check that.
// a string const str = "Hello 😃, How are you?"; // check if How is present in str const isPresent = str.includes("How"); console.log(isPresent); // true
includes() method accepts:
- a string to search for as the first argument
- an optional position of type
numberto start searching from that position
- the method returns boolean
trueis the string present and
See this example live in JSBin.