JavaScript String indexOf() Method | jquery indexof
ExampleSearchastringfor"welcome":letstr="Helloworld,welcometotheuniverse.";str.indexOf("welcome") //Returns13str.indexOf("Welcome") //Returns-1TryitYourself»[1]More"TryitYourself"examplesbelow.DefinitionandUsageTheindexOf()methodreturnsthepositionofthefirstoccurrenceofaspecifiedvalueinastring.indexOf()returns-1ifthevalueisnotfound.indexOf()iscasesensitive.Tip:AlsolookatthelastIndexOf()[2]method.BrowserSupportindexOf()isfullysupportedinallbrowsers:ChromeIEEdgeFirefoxSafariOperaYesYesYesYe...
ExampleSearch a string for "welcome":
let str = "Hello world, welcome to the universe.";str.indexOf("welcome") // Returns 13str.indexOf("Welcome") // Returns -1
Try it Yourself »[1]More "Try it Yourself" examples below.
Definition and UsageThe indexOf() method returns the position of the first occurrence of a specified value in a string.
indexOf() returns -1 if the value is not found.
indexOf() is case sensitive.
Tip: Also look at the lastIndexOf()[2] method.
Browser SupportindexOf() is fully supported in all browsers:
Chrome IE Edge Firefox Safari Opera Yes Yes Yes Yes Yes Yes Syntaxstring.indexOf(searchvalue, start)
Parameter Values Parameter Description searchvalue Required. The string to search for start Optional. Default 0. At which position to start the search Technical Details Retur...