迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript 中检查数组是否不包含某个值

作者:迹忆客 最近更新:2022/12/20 浏览次数:

要检查数组是否不包含值,请使用逻辑 NOT ! 运算符来否定对 includes() 方法的调用。 NOT ! 运算符在对真值调用时返回 false,反之亦然。

const arr = ['a', 'b', 'c'];

const notIncludesD = !arr.includes('d');
console.log(notIncludesD); // 👉️ true

const notIncludesC = !arr.includes('c');
console.log(notIncludesC); // 👉️ false

if (notIncludesC) {
  console.log('✅ the value c is NOT included in the array');
} else {
  console.log('⛔️ the value c is included in the array');
}

JavaScript 中检查数组是否不包含某个值

我们使用逻辑 NOT ! 运算符来否定对 Array.includes 方法的调用。

这种方法允许我们检查特定值是否不包含在数组中。

我们的第一个示例检查值 d 是否不包含在数组中并返回 true

const arr = ['a', 'b', 'c'];

const notIncludesD = !arr.includes('d');
console.log(notIncludesD); // 👉️ true

const notIncludesC = !arr.includes('c');
console.log(notIncludesC); // 👉️ false

字符串 c 包含在数组中,因此表达式返回 false。

以下是使用逻辑 NOT ! 运算符的更多示例。

console.log(!true); // 👉️ false
console.log(!false); // 👉️ true

console.log(!'hello'); // 👉️ false
console.log(!''); // 👉️ true
console.log(!null); // 👉️ true

我们可以想象逻辑 NOT ! 运算符首先将值转换为布尔值,然后翻转值。

当我们否定一个假值时,运算符返回真。 在所有其他情况下,它返回 **false**。

JavaScript 中的假值有:nullundefined空字符串NaN0false

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便