In JavaScript String Object has a function indexOf that is most often used to get the position of specified string pattern passed in the indexOf function. JavaScript indexOf function returns the index position for the first occurrence of specified string pattern in the provided string. By default IndexOf function starts searching from 0th index of the string. If you will pass the optional parameter of start index then it starts searching the string from the specified index.

You can use the sample example string that we used in the previous article about JavaScript String Functions-1:

var myString = new String();

myString = "JavaScript String Function";

myString variable is assigned a string to perform the string functions:

JavaScript IndexOf String Function Syntax

stringObject.indexOf([string pattern], [OPTIONAL start index]);

Exmaple of JavaScript IndexOf Function without optional parameter

document.write( myString.indexOf("ri") );

Above example string will return the index of first occurrence of ri pattern in the provided string.

Exmaple of JavaScript IndexOf Function with startIndex value passed

document.write( myString.indexOf("ri",7) );

Continue to next tutorial: Javascript String LastIndexOf Function to learn how to get the index of specified string pattern that occurs nearest to the last index of the provided string.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.