迹忆客 专注技术分享

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

在 JavaScript 中检查字符串是否相等

作者:迹忆客 最近更新:2023/03/09 浏览次数:

在 JavaScript 中,有四种运算符可用于检查字符串是否相等。这些运算符称为比较运算符

这些运算符不仅用于检查字符串的相等性,还用于检查其他数据类型的相等性。在这四个运算符中,让我们根据用例找出检查字符串相等性的最佳运算符。

你可能已经看过第三个和第四个运算符,即 ==!=,它们在其他编程语言中也很常见。但是它在 JavaScript 中的工作方式与其他语言略有不同。

与 JavaScript 相关,以下是一些用例,你可以在其中使用 ===== 运算符。

使用严格不等于 (!==) 和不等于 (!=) 运算符时,上述规则同样适用。要了解有关具体规则的更多信息,你可以阅读 ES5 规范的 11.9.3 部分。

让我们举一些例子,详细了解这些东西。

在这个例子中,我们采用了两个变量,name_1name_2。它们都以 "adam" 作为字符串值。现在让我们应用上述每个运算符并查看我们得到的输出。

在这里,我们使用了四个 ifelse 语句,每个语句代表不同的比较运算符。由于 name_1name_2 变量的值和数据类型相同,三重等于或严格等于运算符 (===) 将打印 True 作为输出。双等号 == 也将打印 True,因为两个变量具有相同的值。

let name_1 = "adam";
let name_2 = "adam";

// Strict Equal
if(name_1 === name_2) console.log("True");
else console.log("False");

// Equal
if(name_1 == name_2) console.log("True");
else console.log("False");

// Strict not equal
if(name_1 !== name_2) console.log("True");
else console.log("False");

// Not equal
if(name_1 != name_2) console.log("True");
else console.log("False");

输出结果:

True
True
False
False

当使用 Strict 不等于 (!==) 运算符和不等于 (!=) 运算符时,它们都会打印 False 作为输出,因为两个变量中的值本身是相同的。

下面是另一个示例,我们将字符串与整数进行比较。在这里,我们有一个变量 str,其值为 "80",一个字符串值。我们还有另一个变量 num,它包含一个整数值 80 。由于这两个变量具有不同数据类型的相同值,让我们看看对它们使用各种比较运算符时会发生什么。

let str = "80";
let num = 80;

// Strict Equal
if(str === num) console.log("True");
else console.log("False")

// Equal
if(str == num) console.log("True");
else console.log("False")

// Strict not equal
if(str !== num) console.log("True");
else console.log("False")

// Not equal
if(str != num) console.log("True");
else console.log("False")

输出结果:

False
True
True
False

和第一个例子一样,我们也有四个 ifelse 语句,每个语句代表不同的比较运算符。

当我们使用严格等于或三重等于 (===) 时,我们将得到 False 作为输出。原因是即使值相同,数据类型也不同。但是当我们使用双等号 (==) 时,我们得到 True 作为输出,因为双等号只检查值而不是类型。

当我们使用 Strict not equal (!==) 时,我们得到 True 作为输出,因为字符串值 "80" 不等于整数值 80;因此,它返回 True

最后,不等于 (!=) 运算符仅比较两个变量的值。这些值需要不同才能打印 True。由于在这种情况下值相同,因此返回 False

转载请发邮件至 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

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便