QUESTION: How does == vs === work?
Comparison operators in JavaScript play a key role in branching and decision-making logic, and the choice between the loose equality operator and the strict equality operator significantly affects the program's execution result. The double equality operator performs a comparison with preliminary type coercion if the operands belong to different categories. This means the interpreter tries to convert both values to a common data type before matching them. For example, the expression '5' == 5 will return the boolean value true because the string value is converted to a numeric one. This behavior often leads to unexpected results and hard-to-detect bugs in the code due to implicit conversions.
In contrast, the triple equality operator performs a strict comparison without any type coercion. If the compared values belong to different data types, the operator immediately returns false, even if their string representations match. For example, the expression '5' === 5 will return false because the left operand is a string and the right one is a number. In the professional developer community, a golden rule has been established: always use strict equality to avoid logic errors and ambiguities. An exception might only be checking for null and undefined simultaneously via comparison with null using the double equality operator, although in modern projects explicit checks or the nullish coalescing operator are more commonly applied.