Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use indexof method in javascript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use the indexof method in javascript. It is very detailed and has a certain reference value. Friends who are interested must finish it!

The indexof () method in javascript is used to find the specified element in the array. It returns the index of the first match of the specified element value in the array, or "- 1" if the specified value is not found. Using this property, this method can also be used to determine whether the specified element exists in the array.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

JavaScript indexOf () returns the index of the 1st match of an element's value in the array, or-1 if the specified value is not found. The usage is as follows:

Array.indexOf (searchElement [, fromIndex])

Parameter description:

Array: represents an array object.

SearchElement: required parameter, the value to locate in the array.

FromIndex: optional parameter for the array index to start the search. If this parameter is omitted, the search starts at index 0. Returns-1 if fromIndex is greater than or equal to the length of the array. If fromIndex is negative, the search starts at the location of the array length plus fromIndex.

The indexOf () method performs the search by an ascending index, that is, from left to right. When retrieving, the array element is compared congruently with the searchElement parameter value.

Example 1: returns the location where the specified element first appeared, or-1 if it is not found

Var a = ["ab", "cd", "ef", "ab", "cd"]; console.log (a.indexOf ("cd")); / / 1console.log (a.indexOf ("cd", 2)); / / 4console.log (a.indexOf ("gh")); / /-1console.log (a.indexOf ("ab",-2)); / / 3

Output result:

Example 2: use the indexOf () method to determine whether a specified element exists. [for more methods, you can read the article "checking whether a given element exists in JS array learning"]

Var a = ["ab", "cd", "ef", "ab", "cd"]; var b = a.indexOf ("cd"); if (b > 0) {console.log ("given element exists");} else {console.log ("given element does not exist");}

Output result:

The above is all the content of the article "how to use the indexof method in javascript". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report