Question
What is the difference between triple equals, ===
, and double equals, ==
, in JavaScript?
Answer
While these are both comparison equality operators, the triple equals, ===
, is what’s called a strict equality operator while the double equals is an equality operator.
The strict equality operator will compare both the value and type of the operands (the values on the left/right sides of the operator). If the value is the same but the type is not, the equality will evaluate to false
.
For example:
```4 === “4” //will evaluate to `false`
as the left operand is of type number while right operand is of type string`
However, the following code block which uses the equality operator instead will evaluate to true
. This is because the operands of the equality operator will be converted to the same type (if they are not already) before the values of the operands are compared.
4 == "4" //will evaluate to `true`