DEV Community

Discussion on: JS == vs ===

Collapse
 
awwsmm profile image
Andrew (he/him)

=== vs. == is how JavaScript implements equality vs. equivalence. Things that are equal are exactly the same, while things that are equivalent are more-or-less the same.

You could say that 4 and 2+2 are equal, while "four" and 4 are equivalent. The former is a mathematical equality while the latter is a linguistic equivalence.

In Java, for instance, you can use == to test for equality, which checks that the objects you're comparing are the exact same object, stored at the same address in memory. But .equals() checks for equivalence, checking that two objects' fields are equal, without caring that the two objects are in fact the exact same instance.