You can find the first index of a substring in a string in JavaScript using the indexOf()
or search()
methods. Here's how you can use them:
Using indexOf()
:
index.tsx90 chars4 lines
In the above example, the indexOf()
method returns the index of the first occurrence of the letter 'o' in the string 'Hello, World!'.
Using search()
:
index.tsx89 chars4 lines
In the above example, the search()
method returns the index of the first occurrence of the letter 'o' in the string 'Hello, World!'.
Both methods will return -1 if the given substring is not found in the string.
gistlibby LogSnag