To search for a string inside another string, you can use the includes()
method on the string in JavaScript.
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
The includes()
method accepts:
- a string to search for as the first argument
- an optional position of type
number
to start searching from that position - the method returns boolean
true
is the string present andfalse
if not.
See this example live in JSBin.