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

JS determines whether an array contains a certain value

WJ
Release: 2020-05-30 18:01:46
Original
10767 people have browsed it

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)
Copy after login
ParametersDescription
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:

<html>
<body>
<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
</script>
</body>
</html>
Copy after login

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!

Related labels:
js
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template