


Introduction to the use of includes methods in JavaScript arrays and strings
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.
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(元素)
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>
The running result is as follows
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)
The running results are as follows.
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(搜索特定字符串)
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)
The execution results are as follows
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)
The running result is as follows
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)
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
