Copyright © tutorialspoint.com

Javascript String - indexOf() Method

previous next

Advertisements


Description:

This method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex or -1 if the value is not found.

Syntax:

string.indexOf(searchValue[, fromIndex])

Here is the detail of parameters:

Return Value:

Returns the index of the found occurrence otherwise -1 if not found.

Example:

<html>
<head>
<title>JavaScript String indexOf() Method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new String( "This is string one" );
var index = str1.indexOf( "string" );
document.write("indexOf found String :" + index ); 

document.write("<br />");

var index = str1.indexOf( "one" );
document.write("indexOf found String :" + index ); 

</script>
</body>
</html>

This will produce following result:

indexOf found String :8
indexOf found String :15 

To understand it in better way you can Try it yourself.


previous next

Copyright © tutorialspoint.com