JavaScript Comparison Operators: Loose Equality (==) vs Strict Equality (===), Explained w/ Examples

Do you want to know the difference between the double equals and triple equals comparison operators in JavaScript? Then this article might help you.

Nitin Gutte
JavaScript in Plain English

--

Photo by NeONBRAND on Unsplash

In JavaScript, there are two different types of comparison operators available to perform an equality operation.

But first, you need to understand the concepts of truthy and falsy values.

Concept of Truthy and Falsy values:

Falsy values: in which we try to convert values using boolean, then the result always becomes false.

Truthy values: in which we convert values using boolean, then the result always becomes true.

In JavaScript, there are only six types of Falsy values.

  • false: When it is not true and the result is false (boolean value).
  • 0: Zero.
  • “”: If we don't pass any values in the string, it is known as an empty string.
  • null: is the value in which it is empty or not initialized or not defined, or it has a zero value.
  • undefined: is the variable in which it is not assigned any value during its creation phase.
  • NaN: refers to Not-a-Number, it used to indicate an error to enter a valid number.

Example :

Double Equals or Loose Equality (==) :

In loose equality, two different types or the same values are compared to perform the equality operation. First, any one of the two operands is converted to the desired type using the implicit type coercion method.

It is also called Abstract equality.

Example 1 :

In the first example above, first, the string value ‘2’ is converted to number 2 automatically by JavaScript and then the JavaScript compares two values using loosely equality(==) operator.

If both values are the same and there is no need to type coercion in the second example.

Example 2 :

The empty string (‘’) is converted to 0 using coercion by JavaScript and then JavaScript moves to equality operation, then the output is true.

The comparison between the same operands as null and undefined is always true.

Triple Equals or Strict Equality (===) :

If both operands have the same type and same value then the values are said to be equal i.e. the output is true.

In strict equality, there is no coercion operation performed. If one value is totally different from another value, Then the output will be always false.

Example 1 :

In the given examples the output is true. Because they have the same type and same values.

Example 2 :

If both operand values are the same, but the type is different, the JavaScript engine shows the output as false.

Conclusion :

Loose equality and strict equality operators are totally different from each other. It depends on you to choose which type is best suited for your code.

If you use loose equality then definitely you will face unexpected errors in your code. Because implicit coercion takes place before the JavaScript moves to comparison in the code.

Remembering the six types of falsy values is quite confusing and hard to understand, loose equality.

It is always recommended to use strict equality or triple equals in the code to avoid errors.

To learn more about type conversion and coercion, read the following article.

If you enjoyed this post, then feel free to clap, comment, and share with your friends. Don’t forget to follow me for more JavaScript-related articles.

Thank you for reading!

More content at plainenglish.io

--

--

Cybersecurity and Blockchain Enthusiast. Learning to code by teaching others in a simple way. Completed my Diploma in Information Technology.