JavaScript String indexOf()
Syntax & Examples
String.indexOf() method
The indexOf() method of the String class in JavaScript returns the index within the calling String object of the first occurrence of the specified value, searchString, starting the search at fromIndex. Returns -1 if the value is not found.
Syntax of String.indexOf()
There are 2 variations for the syntax of String.indexOf() method. They are:
indexOf(searchString)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchString | required | The string to search for within this string. |
This method returns the index within the calling String object of the first occurrence of searchString.
Returns value of type Number
.
indexOf(searchString, fromIndex)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
searchString | required | The string to search for within this string. |
fromIndex | optional | The position in the calling string to start the search from. Defaults to 0. |
This method returns the index within the calling String object of the first occurrence of searchString, starting the search at fromIndex.
Returns value of type Number
.
✐ Examples
1 Using indexOf() method to find the first occurrence of a substring
In JavaScript, we can use the indexOf()
method to find the first occurrence of a specified substring.
For example,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
indexOf()
method with the argument'world'
to find the first occurrence of the substring. - The result is stored in the variable
index
. - We log
index
to the console using theconsole.log()
method.
JavaScript Program
const str = 'Hello, world!';
const index = str.indexOf('world');
console.log(index);
Output
7
2 Using indexOf() method with a specified start position
In JavaScript, we can use the indexOf()
method to find the first occurrence of a specified substring, starting from a given position.
For example,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
indexOf()
method with the arguments'o'
and5
to find the first occurrence of the character'o'
starting from position5
. - The result is stored in the variable
index
. - We log
index
to the console using theconsole.log()
method.
JavaScript Program
const str = 'Hello, world!';
const index = str.indexOf('o', 5);
console.log(index);
Output
8
3 Using indexOf() method to search for a non-existent substring
In JavaScript, we can use the indexOf()
method to search for a substring that does not exist in the string.
For example,
- We define a string variable
str
with the value'Hello, world!'
. - We use the
indexOf()
method with the argument'JavaScript'
to search for the substring'JavaScript'
within the string. - The result is stored in the variable
index
. - We log
index
to the console using theconsole.log()
method.
JavaScript Program
const str = 'Hello, world!';
const index = str.indexOf('JavaScript');
console.log(index);
Output
-1
Summary
In this JavaScript tutorial, we learned about indexOf() method of String: the syntax and few working examples with output and detailed explanation for each example.