What is Equality in JavaScript

In JavaScript you can use two different operators to check for object equality: == and === They basically do the same thing, but there is a difference between the two.

=== will check for equality of two values. If two values are not of the same type, === will return false.

If they are of the same type, JavaScript will check for equality.

== on the other hand will attempt to convert types to match and check the equality.

The same goes for != and != =, which perform the same thing, but negated.

Below are a few examples where these equality operators can be seen in use:

false == ‘0’ //true

false === ‘0’ //false

null == undefined //true

null=== undefined //false

Unless type conversion is explicitly desired, it’s typically best to use === and == over == and !=, to avoid any unintended side-effects of type conversions.

Leave a Reply

Your email address will not be published.