JavaScript

Which Equals Operator (== vs ===) Should be Used in JavaScript Comparisons?

Comparisons are vital to examine the differences between different values. This approach helps the developers to perform the calculations based on the performed comparison while programming in JavaScript. For this purpose, there are various operators, including the “equality(==)” and “strict equality(===)” operators.

In this write-up, we will differentiate the functionalities of both the equality(==) and strict equality(===) operators.

Which Equals Operator (== vs ===) Should be Used for Comparison in JavaScript?

Both the “equality(==)” and “strict equality(===)” operators give the result in boolean form. The equality operator(==) is utilized to compare the operands or expressions. whereas the strict equality operator(===) checks if the operands and their data type are equal.

Example
Let’s try to understand the stated concept with the below-given example:

<script>
console.log("Comparison of (==) and (===) operators on Different Values")
console.log("(7 == '7') are equal : ", 7 == '7');
console.log("(7 === '7') are strict equal : ", 7 === '7');
console.log("(null == undefined) are equal : " , null == undefined);
console.log("(null ===undefined) are strict equal : ", null === undefined)
</script>

In the above code snippet:

  • First of all, apply the “equality (==)” and the “strict equality (===)” operators upon the stated values.
  • In this example, the equality operator(==) will return “true” since the values are the same.
  • The strict equality operator(===) will return “false” since the datatype is not identical.

Output

It can be seen that the corresponding boolean values are returned based on the values.

Example 1: Utilization of Equality Operator(==) in JavaScript
In this example, the equality operator will be used to compare two values having different data types:

<script>
let a ='4';
let b = 4;
if (a == b) {
 console.log('a and b are equal.');
}
else{
 console.log('a and b are not equal.');
}
</script>

In the above code lines:

  • Firstly, initialize the variables named “a” and “b” having a string and an integer value, respectively.
  • After that, utilize the “if/else” condition and the “equality operator(==)” to apply a check upon the initialized values.
  • Since the equality operator is not concerned about the data types. Hence, the former condition will execute.

Output

In the output, it can be seen both the initialized values are the same.

Example 2: Utilization of the Strict Equality Operator(===) in JavaScript
In this example, the same code will be repeated with the “strict equality(===)” operator with the values comprising the conflicting data types:

<script>
let a ='4';
let b = 4;
if (a === b) {
 console.log('a and b are strictly equal.');
}
else{
 console.log('a and b are not strictly equal.');
}
</script>

In the above code snippet:

  • Likewise, initialize a string and an integer value, respectively.
  • After that, apply the “strict equality(===)” operator upon the defined values in the “if/else” condition.
  • In this scenario, the latter condition will be executed since the data types of both values are not the same.

Output

The core difference can be figured out by analyzing this particular output.

Conclusion

The “equality(==)” operator compares the operands only, whereas the “strictly equality(===)” operator compares both the operands and their data type. The functionalities of both operators are demonstrated with the help of various examples.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.