indexOf() and lastIndexOf()

indexOf() and lastIndexOf()

Add includes() to the mix and we can try to skirt RegExp, yay!

·

3 min read

indexOf

Very similarly to search(), indexOf() will return a string's first appearance within another string.

indexOf(), however, takes a string and not a regular expression as its first parameter. If no string is provided you'll get a 0 back because indexOf() will just act as if you were looking for the original string, which starts at index 0.

The second parameter it accepts is a number, which represents the index position it'll start searching from, until the end of the string.

If no second parameter is provided, if you provide a negative number, or if the number is greater than the length of the string, indexOf() will just start from index 0.

Do note that indexOf() is case-sensitive!

If nothing is found, you'll get -1 back.

let string = 'The Door controls Time and Space! The Door can see into your mind!'

string.indexOf('Door')
//output: 4

string.indexOf('door')
//output: -1

string.toLowerCase().indexOf('door')
//output: 4

// Since the first 'Door' occurrence starts at [4], searching from 5 will find the second one
string.indexOf('Door', 5)
//output: 38

string.indexOf('Door', '5')
//output: 38

//'Door' doesn't appear again in the string after [38], so -1 will be returned
string.indexOf('Door', 39)
//output: -1

// 999 is greater than the original string length, and a search won't even happen
string.indexOf('Door', 999)
//output: -1

// Negative numbers will just default to 0
string.indexOf('Door', -10)
//output: 4

// The non-integer part will not be considered
string.indexOf('Door', 4.94)
//output: 4

lastIndexOf

lastIndexOf() takes two parameters, a string you want to find positioned inside another string, and the position to start looking. It will return -1 if nothing is found.

Unlike indexOf(), this one will find the last occurrence of the searched string rather than the first. This does mean that it will search from the end of the index to the beginning, and so there are a couple of changes when it comes to the second parameter compared to what we saw above.

The second parameter, if not provided, will default to Infinity. It'll search backwards and try to find your searched string from a position lesser than or equal to the number you provide.

If you use a number smaller than 0 then lastIndexOf() will just look for the string at 0 and stop there, since it has no more "backwards" to go to.

Let's use the same parameters as we did in indexOf() and see how they compare:

let string = 'The Door controls Time and Space! The Door can see into your mind!'

string.lastIndexOf('Door')
//output: 38

string.lastIndexOf('door')
//output: -1

string.toLowerCase().lastIndexOf('door')
//output: 38

// Since the last 'Door' occurrence starts at [38], searching from 5 will find the first one
string.lastIndexOf('Door', 5)
//output: 4

//'Door' doesn't appear again in the string before [4], so -1 will be returned
string.lastIndexOf('Door', 3)
//output: -1

// 999 is greater than the original string length, and search will carry on normally from the end of the string
string.lastIndexOf('Door', 999)
//output: 38

string.lastIndexOf('Door', '999')
//output: 38

// Negative numbers will just default to 0, but nothing is found there
string.lastIndexOf('Door', -10)
//output: -1

// There is, however, a "The" which starts at 0
string.lastIndexOf('The', -10)
//output: 0

// The non-integer part will not be considered
string.lastIndexOf('Door', 4.94)
//output: 4

Practice!

If you're interested, this 8-kyu CodeWars kata goes over the topic and is a cool exercise.