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.
foo
和bar
的值和类型相同。 因此,结果对两个都是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
.
这里的number
和stringNumber
的值看起来类似。 然而,种类number
为Number
和类型stringNumber
是string
。 即使值相同,类型也不相同。 因此, ==
检查将返回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
值相同。 之所以如此,是因为0
和false
在JavaScript中具有相同的值,但是在检查类型和值时,该值是false,因为0
是number
而false
是boolean
。
范例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上关注我以获取更新。
javascript运算符