How can I check if a string contains another in Javascript?

Question:

I would like to check if one string contains another, as if it were a String.contains() method but apparently there is no method that does this.

Does anyone know of a similar method that does the same thing?

Answer:

Use the indexOf function:

var s = "foo";
alert(s.indexOf("oo") != -1);

The indexOf function returns the position of a string in another string or -1 if not found.

Scroll to Top