在JavaScript中,有多种方法可以检查一个数组是否包含某个元素。以下是几种常见的方法:
- 使用
includes()
方法
includes()
方法是ES6中引入的一个新方法,用于判断数组是否包含某个元素,并返回布尔值。
const array = [1, 2, 3, 4, 5];
const element = 3;
const isIncluded = array.includes(element); // true
在JavaScript中,有多种方法可以检查一个数组是否包含某个元素。以下是几种常见的方法:
includes()
方法includes()
方法是ES6中引入的一个新方法,用于判断数组是否包含某个元素,并返回布尔值。
const array = [1, 2, 3, 4, 5];
const element = 3;
const isIncluded = array.includes(element); // true