Corsi registrati su
Corsi in diretta per la formazione di Front End Developer e Back End Developer
Returns a numeric value that represents the position of the element in the string. If it doesn’t find the value, it returns -1.
In particular, the string represents where to look for the value. Where the value, that is the first argument passed as a parameter, can be a word, a number or a sentence. If more than one occurrence of this value is found, the method still returns the index of the first value found.
The second parameter, beginning, represents the starting point to carry out the search, as in some cases it may be necessary not to start from the beginning. If not specified, it starts from 0.
Let’s take a simple example of using this method.
We create a sentence and through the indexOf method we search for the word thought, for example.
As a result in the console we will find the value 34 which indicates the position of the word thought.
For example, if the sentence were to contain the word thought several times, it would always give position 34.
So in this case in the console we will see as a result -1 as the value passed as argument is not contained in the string.
Some useful links