Home  >  Article  >  Web Front-end  >  What does the indexOf() method in JavaScript do?

What does the indexOf() method in JavaScript do?

清浅
清浅Original
2018-12-07 17:28:345040browse

The indexOf() method in JavaScript is mainly used to return the position where the specified string value first appears in the string. If not, it returns -1

In JavaScript we The indexOf() method is often used to return the position where a certain string value first appears in the string to simplify our code. Next, in the article, we will introduce in detail how to use the indexOf() method

【Recommended courses: JavaScript Tutorial

What does the indexOf() method in JavaScript do?

indexOf ()

Returns the position where a specified string value first appears in the string. It has two parameters:

searchvalue: refers to the value to be retrieved String value must be written

fromindex: refers to the position where the search starts in the string. Its value can only be an integer, and its legal range is 0 to string.length-1. It can be written or not. If not written, it means that the string will be retrieved from the beginning of the string.

Meaning:

string.indexOf(searchvalue,fromindex)

This means that the string string will be retrieved from beginning to end. , to see if it contains the substring searchvalue. The starting position of the search is at the fromindex of the string (when fromindex is 0, it means that the search will start from the beginning of the string). When a searchvalue is found, the position of the first occurrence of the searchvalue is returned. Character positions in string start from 0.

Note: The indexOf() method is case-sensitive and returns -1 if the string value to be retrieved does not appear.

Example: Get the value in the string

 <script>
var str="hello world"
console.log(str.indexOf("Hello"))
console.log(str.indexOf("World"))
console.log(str.indexOf("world"))
 </script>

The result is as follows:

What does the indexOf() method in JavaScript do?

Example: Use indexOf() to get the value in the array

  <script>
    var subject = ["Chinese","Math","English"];
    console.log(subject.indexOf("Math"));
    console.log(subject.indexOf("music"))
     </script>

The result is as follows:

What does the indexOf() method in JavaScript do?

Because the array starts from 0, the Math position is at 1, and there is no music in the array, so the returned -1.

Summary: The above is the entire content of this article. I hope that through this article, I can have a certain understanding of the indexOf() method.

The above is the detailed content of What does the indexOf() method in JavaScript do?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more