EN
JavaScript - indexOf() method from position
0 points
In this article, we would like to show you how to use indexOf()
method starting from a specific position in JavaScript.
The indexOf()
method with specified second parameter - position, returns the index of the first occurrence of the specified string at a position greater than or equal to position
(which is 0
by default)
.
Runnable example:
xxxxxxxxxx
1
// index: 0 2 5
2
// | | |
3
const text = 'This is example text...';
4
const result = text.indexOf('is', 3);
5
6
console.log(result); // 5
Output:
xxxxxxxxxx
1
5
For comparison, if we had not specified the position
parameter, the method would return the index of the first 'is'
occurrence, which is 2
.
xxxxxxxxxx
1
// index: 0 2 5
2
// | | |
3
const text = 'This is example text...';
4
const result = text.indexOf('is');
5
6
console.log(result); // 2
Output:
xxxxxxxxxx
1
2