Home>Article>Web Front-end> JS determines whether an array contains a certain value

JS determines whether an array contains a certain value

WJ
WJ Original
2020-05-30 18:01:46 10655browse

JS determines whether an array contains a certain value

#How does js determine a value in an array?

The function in js to determine whether an array contains a certain value is "indexOf". The specific usage is as follows:

The indexOf() method returns the first occurrence of a specified string value in a string.

Syntax

stringObject.indexOf(searchvalue,fromindex)
Parameters Description
searchvalue
Required. Specifies the string value to be retrieved.
fromindex
Optional integer parameter. Specifies the position in the string to begin searching. Its legal values are 0 to stringObject.length - 1. If this parameter is omitted, the search will start from the first character of the string.

Note: The indexOf() method is case-sensitive!

Note: If the string value to be retrieved does not appear, this method returns -1.

Specific usage:

First create an html file, add the following code:

    

Then run the html file in the browser

Finally, check the running results. "-1" means that this value does not exist;

Related references:JS Tutorial

The above is the detailed content of JS determines whether an array contains a certain value. 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
Previous article:How to clear timer in js Next article:How to clear timer in js