Table of Contents
*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.
One question commonly presented to JS candidates is the difference between == and === comparisons. Unfortunately, this can often take you off-guard a bit, even if you?re aware of the basic differences. To use these comparisons properly, an in-depth understanding of their functional and performance differences is worthwhile.
Basic Differences Between == and ===
- == operator compares the values of the two variables, making any type corrections needed to make the variables a common type
- === operator not only compares the values, but the types as well, with no transformation of the object types
Due to this transformation function, the == operator is referred to as using type-converting or loose equality.
The === operator is referred to as utilizing strict equality, since both the type and values must be equal in order to result in a true condition.
- == is known as double equals, loose equality, and abstract equality
- === is referred to as triple equals and strict equality
Getting Down to the Details
Both == and === are relational operators, meaning that their function is to compare two variables, returning either a true or false result.
Var x = 123; generates an integer data type
Var y = ?456?; results in a string data type
X+y = ? (the resulting value will be ?123456?
So what is type coercion?
Using the same variables, but equal values of 123 and ?123?, you will find that:
If (x==y) will result in true, since the interpreter will translate the variables into the same data type before comparing the values. However:
If(x===y) will result in a false test, since they are different data types, and the interpreter will not do any type coercion for the === operation.
Which to Use, == Or ===?
Generally speaking, it is best to utilize strict equality (===), but you need to be aware of some specific nuances of the comparison:
Strict Equality (===)
- As you now understand that === will not perform type transformation, it?s a given that any differences between types will result in a false comparison, even if the content values are the same.
- Next, consider that if the two data types are NOT numbers, they will be considered equal (true) if their values are the same.
- Last, if both variables are numbers, they will be considered equal (true) if neither is not a number (NaN) ? utilized in floating point calculations, they have the same value, and even if one has a value of +0 and the other has a value of -0. Strict equality treats negative zero and positive zero as equal values, which is a valid comparison in most situations.
- With strict equality, NaN is unequal to any other value, even itself.
Loose Equality (==)
Most developers agree that strict equality is the best comparison to use, due to its predictability and lack of any type coercion.
When Should You Use ==?
So the consensus on many sides related to when to use ?==? is ? just don?t.
Comparisons ? Emphasis on Use of Strict Equality
For strict equality to return a true response, the following conditions must exist:
- Two number variables are equal when they have the numeric value. Positive and negative zero are equal to each other. NaN is not equal to any other value, even NaN.
- Comparing two Boolean operands results in an equal comparison if both are false or both are true.
- Two strings satisfy the rules of strict equality if they have the same length, the same sequence of characters, and the same characters in the same positions.
- Two objects that refer to the same object are strictly equal.
- Undefined and Null types are ==, but not ===.
Performance Implications of == and ===
Are there any considerations for application performance in choosing loose equality over strict equality?
However, benchmarks to evaluate differences between == and === performance have not always been consistent with that premise. Still, the majority of developers and analysts seem to have both the experience and the benchmarks that bear out at least a slight performance advantage to use of === functions over ==.
From a developer view, it makes perfect sense to provide consistent, readable code that generates anticipated results. Where possible, utilize consistent types of variables, to further predict comparison results.
Keywords: Difference between == and === explained, difference between == and ===