简体   繁体   English

`=` 和 `==` 操作符和 `===` 有什么区别? (单、双、三等号)

[英]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 !==检查两个参数是否不相等类型不同


one more多一个

> checks if one parameter is greater than the other >检查一个参数是否大于另一个

>= checks if one parameter is greater than or equal to the other >=检查一个参数是否大于或等于另一个

>== DOESN'T EXIST >==不存在


etcetera...等等……

== 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.这不仅比较值,还比较类型。

Assignment Operators 赋值运算符

Comparison Operators 比较运算符

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 双等于(==)和三等于(===)之间的JavaScript性能差异 - JavaScript performance difference between double equals (==) and triple equals (===) JavaScript 中 != 和 !== 运算符有什么区别? - What is the difference between != and !== operators in JavaScript? JavaScript中的>>和>>>运算符有什么区别 - what is the difference between >> and >>> operators in JavaScript Javascript字符串中的单转义和双转义(反斜杠)之间有什么区别? - What is the difference between a single and double escape (backslash) in Javascript string? JS:这个例子中的这些三元运算符有什么区别 - JS: What is the difference between these ternary operators in this example “rxjs/operators”和“rxjs/internal/operators”有什么区别? - What's the difference between 'rxjs/operators' and 'rxjs/internal/operators'? 运算符“>>”(双箭头)和“|”(单管)在JavaScript中意味着什么? - What do the operators “>>” (double arrow) and “|” (single pipe) mean in JavaScript? angular.equals和_.isEqual之间有什么区别? - What's the difference between angular.equals and _.isEqual? Javascript:使用的区别是什么? 和:用于在函数或对象中指定变量的运算符? - Javascript: What is the difference between usage of . and : operators for specifying a variable in a function or object? Javascript平等三等于但是大于和小于? - Javascript equality triple equals but what about greater than and less than?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM