array.includes()是JavaScript中的内置函数,用于了解数组中是否存在特定元素,因此返回true或false,即如果存在该元素,则返回true或false
用法:
array.includes(searchElement)
参数:
searchElement:是要搜索的元素。
返回值:
它返回一个布尔值,即True或False。
例子:
Input: [1, 2, 3, 4, 5].includes(2); Output: true Input: [1, 2, 3, 4, 5].includes(9); Output: false
JavaScript代码显示array.includes()函数的工作方式:
代码1:
<script>
// Taking input as an array A
// having some elements.
var A = [ 1, 2, 3, 4, 5 ];
// include() function is called to
// test whether the searching element
// is present in given array or not.
a = A.includes(2)
// Printing result of function.
document.write(a);
</script>
输出:
true
代码2:
<script>
// Taking input as an array A
// having some elements.
var name = [ 'gfg', 'cse', 'geeks', 'portal' ];
// include() function is called to
// test whether the searching element
// is present in given array or not.
a = name.includes('cat')
// Printing result of function
document.write(a);
</script>
输出:
false
相关用法
- Javascript Math.pow( )用法及代码示例
- Javascript Array some()用法及代码示例
- Javascript Number()用法及代码示例
- Javascript Symbol.for()用法及代码示例
- Javascript toExponential()用法及代码示例
- Javascript toString()用法及代码示例
- Javascript Math.abs( )用法及代码示例
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript | array.includes() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。