Home >Web Front-end >Front-end Q&A >What does includes return in es6?

What does includes return in es6?

青灯夜游
青灯夜游Original
2023-01-11 16:44:392490browse

includes() in es6 returns a Boolean value. The includes() method is used to determine whether a string/array contains a specified value. The syntax is "ojb.includes(searchvalue, start)"; if a matching value is found, it returns true, otherwise it returns false.

What does includes return in es6?

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

includes() method is used to determine whether the string/array contains the specified value; it will return a Boolean value indicating whether the string/array contains the given value.

Syntax:

ojb.includes(searchvalue, start)
Parameters Description
searchvalue Required, the string/array to be found.
start Optional, set the position to start searching from, the default is 0.

Return value:

##Type DescriptionBoolean Returns true if a matching value is found, false otherwise.

Example: Find the string starting from the 12th index position

 var str = "Hello world, welcome to the Runoob.";
var n = str.includes("world", 12);
document.getElementById("demo").innerHTML = n;

What does includes return in es6?

Example 2: Check whether the array site contains phpcn


let site = ['phpcn', 'google', 'taobao']; 
site.includes('phpcn'); 
// true 
 site.includes('baidu'); 
// false

Compare the indexof method

The indexOf method has two shortcomings

First, it is not semantic enough , its meaning is to find the first occurrence position of the parameter value, so it is necessary to compare whether it is not equal to -1, which is not intuitive enough to express.

The second is that it uses the strict equivalent operator (===) internally for judgment, which will lead to misjudgment of NaN.

[NaN].indexOf(NaN)
 
// -1
 
includes使用的是不一样的判断算法,就没有这个问题。
 
[NaN].includes(NaN)
 
// true

【Related recommendations:

javascript video tutorial, programming video

The above is the detailed content of What does includes return in es6?. 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