Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between = and == in JavaScript? [duplicate]

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.

like image 790
o_O Avatar asked Jun 07 '26 08:06

o_O


2 Answers

= 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.

like image 169
Randall Hunt Avatar answered Jun 08 '26 20:06

Randall Hunt


= 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

like image 37
Naftali Avatar answered Jun 08 '26 20:06

Naftali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!