I have some code I'm writing 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.
=== 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
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With