当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。