Difference between Equality Comparisons == and === in JavaScript

Often developers are confused between the JavaScript Equality Operators. In this article we will look How many types of Equality Comparison Operator are there and What is the difference between Equality Comparisons == and === in JavaScript?

Types of  Equality Comparisons in JavaScript

JavaScript provides three different value-comparison operations:

  • strict equality (or “triple equals” or “identity”) using ===,
  • loose equality (“double equals”) using ==,
  • and Object.is (new in ECMAScript 2015).

Difference between Equality Comparisons == and === in JavaScript

Loose Equality(==)

The double equals will perform a type conversion when comparing two things in JavaScript. This is often considered as the slowest in the JavaScript when you are comparing the two different object types. Many developers do not recommend to use this approach for the comparison.

Strict Equality(===)

The Triple equals will behave almost like == operator but the only difference is it will not perform any type conversion, It just compares two objects and if the type is same it returns true else it will returns false.

The strict equality operator is much faster than loose equality as it doesn’t have to do any type conversion. It is always recommended to use === when you are not sure about the object types.

Object.is

Object.is will behave the same way as triple equals, but with special handling for NaN and -0 and +0 so that the last two are not said to be the same, while Object.is(NaN, NaN) will be true.

Examples of == vs ===

Also, check out the very good example provided by the Mozilla Developer Network.

Equality comparisons and sameness - JavaScript

Difference between Equality Comparisons == and === in JavaScript

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.