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>