[英]What is the difference between the `=` and `==` operators and what is `===`? (Single, double, and triple equals)
I have written some code and in certain places ==
is required and in others =
is required.我已经编写了一些代码,在某些地方需要
==
,而在其他地方需要=
。 Can someone explain the differences or point me in the direction of the resource that can?有人可以解释这些差异或指出我可以使用的资源方向吗?
Example:例子:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I'm changing and in the other I'm checking.我唯一能想到的是,在一个我正在改变,而在另一个我正在检查。 But in both I am referring to equality.
但在两者中,我指的是平等。
=
is the assignment operator. =
是赋值运算符。 It sets a variable (the left-hand side) to a value (the right-hand side).它将一个变量(左侧)设置为一个值(右侧)。 The result is the value on the right-hand side.
结果是右侧的值。
==
is the comparison operator. ==
是比较运算符。 It will only return true
if both values are equivalent after coercing their types to the same type.只有在将它们的类型强制为相同类型后两个值相等时,它才会返回
true
。
===
is a more strict comparison operator often called the identity operator. ===
是一种更严格的比较运算符,通常称为恒等运算符。 It will only return true
if both the type and value of the operands are the same.只有当操作数的类型和值都相同时,它才会返回
true
。
I would check out CodeCademy for a quick intro to JavaScript.我会查看CodeCademy以快速了解 JavaScript。
If you prefer to read more, MDN is a great intro as well.如果您喜欢阅读更多内容, MDN也是一个很好的介绍。
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.对于那些关心术语“身份运算符”来源的人,jbabey 指出JavaScript: The Definitive Guide似乎提到了它。
=
assigns a value to a variable =
为变量赋值
==
checks if the two parameter are equal to each other ==
检查两个参数是否相等
===
checks if the two parameters are equal to each other and if their type is the same ===
检查两个参数是否相等以及它们的类型是否相同
!
not operator不是运算符
!=
checks if the two parameters are not equal to each other !=
检查两个参数是否不相等
!==
checks if the two parameters are not equal to each other or the type is not the same !==
检查两个参数是否不相等或类型不同
>
checks if one parameter is greater than the other >
检查一个参数是否大于另一个
>=
checks if one parameter is greater than or equal to the other >=
检查一个参数是否大于或等于另一个
>==
DOESN'T EXIST >==
不存在
== is used to test if the value on the left is equal to the value on the right. == 用于测试左边的值是否等于右边的值。
= is used to assign the value on the right to the variable on the left. = 用于将右边的值赋给左边的变量。
In javascript you have also the ===.在 javascript 中,您也有 ===。
=
This is for set the value to the variable. =
这是为变量设置值。
==
This is for compare if the value is the same. ==
如果值相同,这是用于比较。
===
This is for compare if the value is the same and also the type is the same. ===
如果值相同且类型相同,则用于比较。
The = operator is an assignment operator. = 运算符是赋值运算符。 You are assigning an object to a value.
您正在将一个对象分配给一个值。 The == operator is a conditional equality operation.
== 运算符是一个条件相等运算。 You are confirming whether two things have equal values.
您正在确认两件事是否具有相等的值。 There is also a === operator.
还有一个 === 运算符。 This compares not only value, but also type.
这不仅比较值,还比较类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.