Predicting the result of an equality check of two or more values in JavaScript has been a part of the language that trips many developers; but not anymore, as this article stops at nothing in providing you with a straightforward and understandable approach to it.
This article does not include explanations for edge case comparisons such as
[] == ""and[undefined] == [null]
Introduction
Determining if any set of values are equal in JavaScript is achieved using any one of these:
- The Abstract Equality Operator (
==) - The Strict Equality Operator (
===) Object.is
The major difference between the strict equality operator and the abstract equality operator is NOT that the strict equality operator checks for the equality of the value types being compared and the abstract equality operator does not; but that the strict equality operator does not allow coercion before comparison, while the abstract equality operator allows coercion before comparison.
Irrespective of the operator used, the result of checking the equality of any set of values is either true (if the values compared are equal) or false (if the values compared are not equal).
Comparison with the Abstract Equality Operator (==)
When comparing the equality of any set of values using ==,
There are seven value types here:
undefined,null,Boolean,Number,String,ObjectandSymbol.
if the value types of any of the set of values to be compared are the same, there is no need for coercion; hence, a strict equality comparison is performed and the result is returned, otherwise;
undefinedandnullvalues are coercively equal to each other; in other words, testing ifundefined == nullwill returntrue.undefinedandnullwill not coerce to any other type (Boolean,Number,String,Object, orSymbol) when==is used to compare their equality with these other types;all
StringandBooleanvalues are first coerced toNumbervalues before a check for the equality of the values is determined. (TheBoolean falseis coerced to+0, whiletrueis coerced to+1.);all
objects (remember thatnullis not anobjectin this case) are coerced to their primitive values before an equality check is carried out.
The primitive value of an object is determined internally by the JavaScript engine, however, depending on the object hint, the object is either coerced to a String (in the case of arrays) or to a Number.
Comparison with the Strict Equality Operator (===)
When comparing the equality of any set of values using ===,
if the value types (
Number,Stringe.t.c) of the set of values under comparison are different, the JavaScript engine avoids coercion immediately and returnsfalse; otherwise,if the set of values under comparison are of type
Stringand they are exactly the same sequence of code units (same length and same code units at corresponding indices), the JavaScript engine returnstrue; otherwise, it returnsfalse.if the set of values under comparison are of the
Booleantype, the JavaScript engine returnstrueif the values are the same, otherwise it returnsfalseif the set of values under comparison are of the type
Symboland they have the sameSymbolvalue, the JavaScript engine returnstrue, otherwise, it returnsfalse;if the set of values under comparison are of the
Objecttype and they reference the same object in memory, the JavaScript engine returnstrue, otherwise, it returnsfalse.if any of the values under comparison is
NaN, the JavaScript engine returnsfalse;+0 and -0 Number values are equal to each other, therefore, return
true.
Practice Time!
What will be the result of the following comparisons?
0 == nullfalse == undefinedtrue == 1'JavaScript' == true'JavaScript' == false
Answers
false.nullwill not coerce to any other value exceptundefinedfalse.falsegets coerced to its Number value (+0), butundefineddoes not get coerced to its Number value (which is NaN). Since +0 is not the same asundefined, false is returned.
You can answer the rest in the comments section, with an explanation for your answer, and also feel free to add your own questions too 😉.
Top comments (0)