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
numberto start searching from that position - the method returns boolean
trueis the string present andfalseif not.
See this example live in JSBin.
Top comments (0)