當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JavaScript includes()用法及代碼示例


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


相關用法


注:本文由純淨天空篩選整理自 includes() function with example in JavaScript。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。