javascript运算符_JavaScript三等号VS二等号–比较运算符示例说明

javascript运算符

You may have seen double and triple equals signs in JavaScript. But what do they mean?

您可能已经在JavaScript中看到了两倍和三倍等号。 但是它们是什么意思呢?

Well in short: == inherently converts type and === does not convert type.

简而言之: ==本质上会转换类型,而===不会转换类型。

Double Equals (==) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other.

Double Equals( == )仅检查值相等。 它固有地执行强制类型转换。 这意味着在检查值之前,它将转换变量的类型以使其相互匹配。

On the other hand, Triple Equals (===) does not perform type coercion. It will verify whether the variables being compared have both the same value AND the same type.

另一方面,三重等于( === )不执行类型强制。 它将验证所比较的变量是否具有相同的值相同的类型。

OK - so let's help you better understand the difference through a few examples. For each of these, consider what the output of these statements will be.

好的-让我们通过一些示例帮助您更好地理解差异。 对于每一个,请考虑这些语句的输出。

范例1: (Example 1:)

const foo = "test" 
const bar = "test"  

console.log(foo == bar) //true
console.log(foo === bar) //true

The value and the type of both foo and bar is same. Therefore the result is true for both.

foobar的值和类型相同。 因此,结果对两个都是true的。

示例2: (Example 2:‌)

const number = 1234 
const stringNumber = '1234'  

console.log(number == stringNumber) //true
console.log(number === stringNumber)  //false

The value of number and stringNumber looks similar here. However, the type of number is Number and type of stringNumber is string. Even though the values are same, the type is not the same. Hence a == check returns true, but when checked for value and type, the value is false.

这里的numberstringNumber的值看起来类似。 然而,种类numberNumber和类型stringNumberstring 。 即使值相同,类型也不相同。 因此, ==检查将返回true ,但是在检查值类型时,该值为false

范例3: (Example 3:)

console.log(0 == false) //true
console.log(0 === false) //false

Reason: same value, different type. Type coercion

原因:相同的值,不同的类型。 类型强制

This is an interesting case. The value of 0 when checked with false is same. It is so because 0 and false have the same value for JavaScript, but when checked for type and value, the value is false because 0 is a number and false is boolean.

这是一个有趣的案例。 选中false时的0值相同。 之所以如此,是因为0false在JavaScript中具有相同的值,但是在检查类型值时,该值是false,因为0numberfalseboolean

范例4: (Example 4: )

const str = ""

console.log(str == false) //true
console.log(str === false) //false

The value of empty string and false is same in JavaScript. Hence, == returns true. However, the type is different and hence === returns false.

空字符串和false的值在JavaScript中相同。 因此, ==返回true。 但是,类型不同,因此===返回false。

什么时候应该使用== ,什么时候应该使用===(When should you use == and when should you use ===?)

When in doubt, use ===. This will save you from a ton of potential bugs.

如有疑问,请使用=== 。 这样可以避免大量潜在的错误。

If you are supporting a use case where you can be a little lenient about the type of incoming data, then use ==. For example, if an API accepts both "true" and true from the client, use ==. In short, do not use == unless you have a strong use case for it.

如果您支持一个对输入数据的类型可能比较宽容的用例,请使用== 。 例如,如果API从客户端接受"true"true ,则使用== 。 简而言之,除非您有强大的用例,否则请勿使用==

Here's a handy JavaScript truth table for your reference, and to show you just how complicated equality is in JavaScript:

这是一个方便JavaScript真值表供您参考,并向您展示JavaScript中的等式多么复杂:

If you enjoyed this article, be sure to follow me on twitter for updates.

如果您喜欢本文,请确保在Twitter上关注我以获取更新。

翻译自: https://www.freecodecamp.org/news/javascript-triple-equals-sign-vs-double-equals-sign-comparison-operators-explained-with-examples/

javascript运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值