indexOf()  String  instance method JS Home  <<  JS Reference  <<  String  <<  indexOf()

Description

Returns the index value for the first occurence of the specified string from the specified starting index.

  • Returns -1 if the specified string value is not found.

Syntax

Signature Description
aString.indexOf(searchValue[, fromIndex])Returns the index value for the first occurence of the specified string from the specified starting index.

Parameters

Parameter Description
searchValueA string value.
fromIndexAn integer between 0 and the length of the string.

Examples

The code below creates a string of the alphabet and returns starting index for specified string value.


// Create alphabet string varible.
var theAlphabet = 'abcdefghijklmnopqrstuvwxyz';

// Return index of first occurence of search string.
alert(theAlphabet.indexOf('jkl'));

Press the button below to action the above code:

Related Tutorials

JavaScript Basic Tutorials - Lesson 8 - Strings