includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。
句法1:
[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false [1, 2, 3].includes(3, 3); // false [1, 2, 3].includes(3, -1); // true [1, 2, NaN].includes(NaN); // true
返回值:返回一个布尔型,判断一个数组是否包含一个指定的值,如果是返回 true,否则false。
JS Array 对象中的includes()方法浏览器的兼容性
| 方法 | Chrome | Inter Explorer | Firefox | Safari | Opera |
|---|---|---|---|---|---|
| includes() | YES | YES | YES | YES | YES |
js array使用includes()检测数组是否包含字符串
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js array使用includes()检测数组是否包含字符串- Break易站(breakyizhan.com)</title>
</head>
<body>
<script>
let site = ['breakyizhan', 'google', 'taobao'];
document.write(site.includes('breakyizhan'));
// true
document.write("<br>");
document.write(site.includes('baidu'));
// false
</script>
</body>
</html>
