The equality operator in javascript is used to compare if two values are equal. The comparison is made by ==
and ===
operators in javascript.
Double Equal (==) Operator:
The double equal('==') operator tests for abstract equality i.e it does the type conversion of the operands before equality comparison.
The ==
operator returns true if both operands are of the same data type and have the same value or if both are of different data types, but either of them can be converted to the data type of the other operand and have the same value. If both operands have different values, then it returns false.
Let's see using example.
- The behaviour of the
==
operator when both operands are of a different type:
If we are about to compare two values, one string, and another number, then it will convert the string value to the number type and then compare the values.
let a = 20;
let b = '20';
console.log(a == b);
//returns true
In the above example, b is converted to number type by the operator, and then it is compared to a. Since the numeric value of both a and b are the same, so it outputs true.
- The behaviour of the
==
operator when both operands are of the same type:
If both operands are of the string type, then the ==
operator will return true only if each element of the first operand matches with each element of the second operand.
let str1 = 'Helloworld';
let str2 = 'Helloworld';
let str3 = 'HelloWorld';
console.log(str1 == str2);
//returns true
console.log(str1 == str3);
//returns false
In the above example, the first output is true as str1 and str2 are the same, but the second output is false as str1 and str3 aren't the same.
Thus we can conclude that the comparison is case-sensitive as well.
- If we are comparing boolean and number (0 or 1), then it treats 0 as false and 1 as true.
- In case we are comparing null and undefined, then
==
operator will return true.
- If we are comparing two objects, then the operator will check if both refer to the same object. If yes, then the
==
operator will return true, otherwise==
will return false.
In the above example, obj2 is pointing to the same memory location as obj1. That's why ==
operator returns true.
Though the value of obj3 is the same as obj1, both are not pointing to the same memory location, so ==
operator returns false.
Triple Equal (===) Operator:
The triple equal('===') operator tests for strict equality i.e it compares the values as well as the data types of the operands.
The ===
operator compares operands and returns true if both operands are of the same data type and have some value, otherwise, it returns false.
Unlike
==
operator,===
doesn't do type conversion.
Let's understand with an example.
let a = 10;
let b = "10";
let c = 10;
console.log(a===b);
//returns false
console.log(a===c);
//returns true
Here the first output is false as a is a number type whereas b is a string type, the second output is true as both a and c have the same data type and value.
- If any one of the two operands that we are comparing is NaN, then it will return false.
- If the operands that we are comparing are null and undefined, then it will return false.
- If the operands that we are comparing are objects, then it will return true if both refer to the same object (same memory location), else it will return false.
Conclusion:
SL No. | \== operator | \=== operator |
1 | Returns true if operands have the same value, and returns false if the values differ. | Returns true only if operands are of the same data type and same value, otherwise returns false. |
2 | In case both operands are of different data types, it performs type conversion of one operand in order to make the data types of the operands the same. | In case both operands are of different data types, it doesn't perform type conversion of the operands. |
3 | Also known as loose equality | Also known as strict equality |
Thanks for reading ๐