Javascript string contains

Share Button

JavaScript String indexOf() Method

Definition and Usage

The indexOf() method returns the position of the first occurrence of a specified value in a string.

This method returns -1 if the value to search for never occurs.

Note: The indexOf() method is case sensitive.



Parameter Values

Parameter Description
searchvalue Required. The string to search for
start Optional. Default 0. At which position to start the search


Find the first occurrence of the letter “e” in a string:

var str = "Hello world, welcome to the universe.";
var n = str.indexOf("e");

The result of n will be:


Check string contains or string index of substring

For checking if an string contains a text or sub string use the following code.

var string = "Some text",
var substring = "tex";
if(string.indexOf(substring) > -1) {
else {
    alert("Did not contains");
Share Button