Javascript indexOf() String Method

Last Updated Jul 29, 2015, 07:00:14 PM






The javascript string indexOf() method is used to return the first occurance of a specific character or value in a string
Syntax

SearchValue is the specific value or character you want to search, for example the character " e " in a word " apple " and fromIndex is the place from you want to start the index for example, you might wish to begin the search from 3rd or 2nd character instead of searching from the beginning.

Starting the search at fromIndex. Returns -1 if the value is not found.

The following example, the javascript indexOf method will find the occurance of characters 'c, D, g, d' in the word "Black Dog" and returns its index value


Example

Try It Now

Case-sensitivity

The Javascript indexOf() method is case-sensitive. Which means if you search for the letter in smaller case then the word will match only for the smaller case, it returns -1 as not even though it has the same letter in uppercase letter

For example in the below example we will try to find the letter occurance of 'd' but it returns -1 as not found even though it has a letter 'D' in uppercase


Try It Now


Sources and Credits

The source of the content has been referred and updated with Mozilla Developer Network and W3C Organization

Last Updated Jul 29, 2015, 07:00:14 PM



Browser compatibility

Feature
indexOf() Yes Yes Yes Yes Yes Yes Yes