Home > Article > Web Front-end > js indexOf() definition and usage_basic knowledge
Returns the character position of the first substring occurrence in the String object.
strObj.indexOf(subString[, startIndex])
Parameters
strObj
Required. String object or literal.
subString
Required. The substring to find in the String object.
starIndex
Optional. This integer value indicates the index within the String object at which to begin the search. If omitted, the search is from the beginning of the string.
Description
The indexOf method returns an integer value indicating the starting position of the substring within the String object. If the substring is not found, -1 is returned.
If startindex is negative, startindex is treated as zero. If it is greater than the largest character position index, it is treated as the largest possible index.
Perform the search from left to right. Otherwise, the method is the same as lastIndexOf.
Example
The following example illustrates the use of the indexOf method.