There are many cases in which JavaScript's type-coercing equality operator is not transitive. For example, see "JavaScript equality transitivity is weird."
However, are there any cases in which ==
isn't symmetric? That is, where a == b
is true
and b == a
is false
?
Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...
The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.
== converts the variable values to the same type before performing comparison. This is called type coercion. === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.
== in JavaScript is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values. Checks the equality of two operands without considering their type. Compares equality of two operands with their types.
It's supposed to be symmetric. However, there is an asymmetric case in some versions of IE:
window == document; // true document == window; // false
In Javascript, ==
is always symmetric.
The spec says:
NOTE 2 The equality operators maintain the following invariants:
A != B
is equivalent to!(A == B)
.A == B
is equivalent toB == A
, except in the order of evaluation ofA and B
.
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