Home > Web Front-end > JS Tutorial > body text

Introduction to the use of includes methods in JavaScript arrays and strings

不言
Release: 2019-01-08 14:57:49
Original
33133 people have browsed it

The includes method is a method used to check whether a specific element is included in an array or a string. It is mainly used to confirm the existence of elements in an array or string. In this article, we will take a look at the use of the includes method.

Introduction to the use of includes methods in JavaScript arrays and strings

First let’s look at the use of the includes method in array(Array)

One of the existing methods of array(Array) object One is the includes method, which is used to check whether a specific element in the array exists.

The basic syntax is as follows.

数组.includes(元素)
Copy after login

includes method only returns true or false.

Returns true if the element specified in the parameter is contained in the array; if not, returns false.

Let’s look at a specific example

The code is as follows

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
<script type="text/javascript">
	var myarray = [1,2,3,4,5];
var res1 = myarray.includes(3);
var res2 = myarray.includes(7);
console.log(res1)
console.log(res2)
</script>
</body>
</html>
Copy after login

The running result is as follows

Introduction to the use of includes methods in JavaScript arrays and strings

is above In the code, we first create the array myarray.

Then we use the includes method to check whether there is an element named "3" in myarray; since myarray contains "3", the variable res1 returns true.

Next, I checked if an element named "7" exists in myarray; since "7" does not exist in myarray, the variable res2 returns false.

In addition, you can also specify the position to start searching for the specified element.

Examples are as follows

var myarray = [1,2,3,4,5];
var res1 = myarray.includes(3,1);
var res2 = myarray.includes(1,2);
console.log(res1)
console.log(res2)
Copy after login

The running results are as follows.

Introduction to the use of includes methods in JavaScript arrays and strings

In the above code, the myarray array is also used.

In the sample code just mentioned, there is only one parameter for the includes method, but this time we also use the second parameter to specify where to start the search.

myarray.includes(3,1) searches for "3" in myarray, the second element is the first.

"3" is the third element, so even if you search from the second element, "3" is present in myarray.

The next step is to search for "1" using the third element first.

However, "1" is the first element of myarray, so if you search only from the third one, "1" will not be there.

It should be noted here that the elements start counting from 0.

How to use the includes method in string (String)

In addition to the include method of arrays, there are also strings (String) that can also use include. Method, which is used to check whether a specific string is present in a string.

The basic syntax is as follows.

字符串.includes(搜索特定字符串)
Copy after login

The return value is only true or false.

Let’s look at a specific example

var mystr = "Hello, world!";
var res1 = mystr.includes("Hello");
var res2 = mystr.includes("nice");
console.log(res1)
console.log(res2)
Copy after login

The execution results are as follows

Introduction to the use of includes methods in JavaScript arrays and strings

The above code is described in the array (Array) The include method has the same functionality.

Since the string Hello exists in the mystr string, true is returned.

However, because the string nice does not exist in mystr, it returns false.

In addition, like arrays, you can specify the position to start searching for the specified element.

The code is as follows

var mystr = "Hello, world!";
var res1 = mystr.includes("Hello", 1);
var res2 = mystr.includes("ello", 1);
console.log(res1)
console.log(res2)
Copy after login

The running result is as follows

Introduction to the use of includes methods in JavaScript arrays and strings

In the above code, we find the string Hello from the second element .

The "element" used here means each character that makes up the string.

So, searching from the first one means in the letters ello, world! Search for the initial letter H.

ello, world! There is naturally no Hello string in the string.

Therefore, false is returned in res1.

However, there is a string ello, so the variable res2 returns true.

One thing to note when using the includes method of a string is that the include method is case-sensitive.

var mystr = "Hello, world!";
var res1 = mystr.includes("hello");
console.log(res1)
Copy after login

The result will be false, in the above code, the string hello is searched, the includes method distinguishes Hello from hello, so it displays false.

There is another method similar to the includes method in JavaScriptindexOf. Regarding the use of the indexOf method, you can refer toHow to use indexOf in JavaScriptThis Article.

The above is the detailed content of Introduction to the use of includes methods in JavaScript arrays and strings. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!