What is == operator?
The == sign (double equals) function is called the equality operator. Its function is to compare two expressions or values. If the expressions are equivalent, then the result is true. Or else, the result is false. However, functions, variables, arrays, and objects are compared with the help of reference. For instance, two variables are equal when referring to a similar array, function or object.
In the above example, two variables assign with the numbers 10 and 5. Then, it checks whether num1 and num2 are equal. When the double equals check by value, it results in a false statement. If both the number values are equal to 10, it returns the true value.
What is === operator?
The == sign (triple equals) is called equality operator. Its function is to check whether or not two operands are equal and return the true or false value. The operator returns false whenever the values are not of a similar type. Conversely, the operator returns true when the values are of a similar type.
As discussed earlier, the operator checks both the type and value of two operands. Whether num1 and num2 are equal or not, it returns the false because the values are the same, but the type is not the same. It is because one is an integer, and the other is a string.
- == operator is called a type coercion operation. As long as both values are the same and compared using == operator, the type coercion will happen. On the other side, === is called the strictly equal operator. It is much similar to the equality operator in Java. It renders the compilation error when comparing two variables whose types are not equal.
Key differences between == and === operator
Here are the significant aspects in which double equals are different from triple equals.
NamingDouble equals: It is named as the equality operator
Triple equals: It is called the strict equality or identity operator
Double equals: (a==b)
Triple equals: (a===b)
Double equals: It accessed as the type converting the conversion.
Triple equals: It is utilized as strict conversion without performing conversion in operands.
Double equals: It converts the operands into the same type and then compares. It means comparison will perform once both operands are of the same type.
Triple equals: It does not perform any type of conversion before comparison and returns true if both operands’ value and type are the same.
Which is faster and provides the best performance == or ===?
In theory, when comparing the variables with identical types, the performance will be the same across both operators. It is because both operators access the same algorithm. On the other hand, when the types are different, the triple equals operator performs much better than double equals as it does not do the extra step of type coercion.
However, you can do some performance tests to know the accurate result. Unless you do not need == operator, you should not use the double equals operator because it may provide you with unexpected results. In short, try to use === operator all the time and everywhere except if you need type coercion.