How to search for a string inside a string in JavaScript?

Published November 3, 2020

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 and false if not.

See this example live in JSBin.

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter