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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。