includes() 是 JavaScript 中的一個預定義函數,用於檢查給定元素是否存在於數組中?
例:
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
//array
var cities = ["New Delhi", "Mumbai", "Chennai", "Banglore"];
//checking whether "New Delhi" exists or not
if(cities.includes("New Delhi")==true)
document.write("New Delhi exists in the array");
else
document.write("New Delhi does not exist in the array");
document.write("<br>");
//checking whether "Pune" exists or not
if(cities.includes("Pune")==true)
document.write("Pune exists in the array");
else
document.write("Pune does not exist in the array");
document.write("<br>");
</script>
</body>
</html>
輸出
New Delhi exists in the array Pune does not exist in the array
相關用法
- JavaScript isNaN()用法及代碼示例
- JavaScript isFinite()用法及代碼示例
- JavaScript Uint8Array.of()用法及代碼示例
- JavaScript BigInt.prototype.toString()用法及代碼示例
- JavaScript DataView.getInt16()用法及代碼示例
- JavaScript Symbol.keyFor()用法及代碼示例
- JavaScript handler.has()用法及代碼示例
- JavaScript JSON.stringify()用法及代碼示例
- JavaScript Symbol.split屬性用法及代碼示例
- JavaScript Function.displayName屬性用法及代碼示例
- JavaScript TypedArray reverse()用法及代碼示例
- JavaScript String slice()用法及代碼示例
- JavaScript 正則 \n用法及代碼示例
- JavaScript Math hypot()用法及代碼示例
- JavaScript Set add()用法及代碼示例
- JavaScript Array fill()用法及代碼示例
- JavaScript Math abs()用法及代碼示例
- JavaScript Date toISOString()用法及代碼示例
- JavaScript DataView.getInt8()用法及代碼示例
- JavaScript dataView.setBigInt64()用法及代碼示例
注:本文由純淨天空篩選整理自 includes() function with example in JavaScript。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。