admin管理员组文章数量:1037775
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.
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.
Share Improve this question edited Nov 9, 2022 at 20:34 Sebastian Simon 19.5k8 gold badges60 silver badges84 bronze badges asked Aug 8, 2012 at 19:18 o_Oo_O 5,73714 gold badges55 silver badges94 bronze badges 8- 2 = is assignment a = b means put b inside a. . == is unstrict equality, a==b means a is roughly equal to b . a===b is strict equality and the one you should be using – Benjamin Gruenbaum Commented Aug 8, 2012 at 19:20
- 4 Any JavaScript language guide will go into details. – Dave Newton Commented Aug 8, 2012 at 19:20
- But to be fair until I asked this I wasn't aware of ===, so now I am in need to know what's the diff so thanks for that link. – o_O Commented Aug 8, 2012 at 19:28
- 2 @RKS This page covers JavaScript expressions in general. – Pointy Commented Aug 8, 2012 at 19:29
- Related: stackoverflow.com/questions/2063480/the-3-different-equals – Jonas Wilms Commented Mar 24, 2018 at 21:22
5 Answers
Reset to default 19=
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.
===
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.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
=
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 ===.
=
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
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.
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.
Share Improve this question edited Nov 9, 2022 at 20:34 Sebastian Simon 19.5k8 gold badges60 silver badges84 bronze badges asked Aug 8, 2012 at 19:18 o_Oo_O 5,73714 gold badges55 silver badges94 bronze badges 8- 2 = is assignment a = b means put b inside a. . == is unstrict equality, a==b means a is roughly equal to b . a===b is strict equality and the one you should be using – Benjamin Gruenbaum Commented Aug 8, 2012 at 19:20
- 4 Any JavaScript language guide will go into details. – Dave Newton Commented Aug 8, 2012 at 19:20
- But to be fair until I asked this I wasn't aware of ===, so now I am in need to know what's the diff so thanks for that link. – o_O Commented Aug 8, 2012 at 19:28
- 2 @RKS This page covers JavaScript expressions in general. – Pointy Commented Aug 8, 2012 at 19:29
- Related: stackoverflow.com/questions/2063480/the-3-different-equals – Jonas Wilms Commented Mar 24, 2018 at 21:22
5 Answers
Reset to default 19=
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.
===
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.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
=
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 ===.
=
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
本文标签:
版权声明:本文标题:javascript - What is the difference between the `=` and `==` operators and what is `===`? (Single, double, and triple equals) - 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1738187663a1553555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论