Home > Article > Web Front-end > 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:
<html> <body> <script type="text/javascript"> var str="Hello world!" document.write(str.indexOf("Hello") + "<br />") </script> </body> </html>
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!