The == operator is really funny. It is usually doesn’t behave as one think it will.
This led me to investigate exactly what is happening below the tip of the iceberg, and according to MDN it is as follow:
If the two operands are not of the same type, JavaScript converts the
operands then applies strict comparison. If either operand is a number
or a boolean, the operands are converted to numbers if possible; else
if either operand is a string, the other operand is converted to a
string if possible. If both operands are objects, then JavaScript
compares internal references which are equal when operands refer to
the same object in memory.
So, why doesn’t "undefined" == undefined evaluate to true?
Shouldn’t undefined be converted to "undefined" and then return true according to this description?
"undefined"has a value. It is the 9 letters: u-n-d-e-f-i-n-e-d. Therefore, the string “undefined” does not have an undefined value. AStringin javascript can have an undefined value, but here theStringobject has a defined value that just happens to spell “undefined”.Using the explanation you’ve provided, the
undefinedvalue on the right side would be converted to aStringobject with no value assigned, and then compared to theString“undefined”, failing the comparison.