js中字符串的includes和数组的includes
时间: 2023-11-24 18:53:18 浏览: 82
JS字符串和数组之间的转换
在JavaScript中,字符串和数组都有一个includes()方法,用于检查一个字符串或数组是否包含另一个字符串或元素。下面是两个例子:
1. 字符串的includes()方法:
```javascript
const str = 'hello world';
console.log(str.includes('world')); // 输出:true
console.log(str.includes('javascript')); // 输出:false
```
2. 数组的includes()方法:
```javascript
const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(3)); // 输出:true
console.log(arr.includes(6)); // 输出:false
```
阅读全文