JavaScript String indexOf() Method
You are Here:
JavaScript String indexOf() Method
The indexOf()
method returns the position of the first occurrence of a specified value in a string.
Note: This method is case sensitive.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var str = "Hello there my old friend"
var result = str.indexOf("fri");
document.write(result);
</script>
</body>
</html>
Syntax
str.indexOf(searchString, startFrom)
Parameter Values
Value | Type | Explanation |
---|---|---|
searchString | Required | Specifies the characters to be searched for within this string. |
startFrom | optional | Specifies the starting position of a string (str). If omitted, the default value is '0'. |
Return Value
Value | Condition |
---|---|
-1 | When the searched value are not found within this string. |
0 to str.length | A index of a search value within this string. |
indexOf() with Specified Starting Position
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<script>
var str = "Hello there my old friend"
var result = str.indexOf("Hello", 10);
// str = e my old friend;
document.write(result);
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in JavaScript.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.