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.
(source)
So, why doesn't "undefined" == undefined
evaluate to true?
Shouldn't undefined
be converted to "undefined"
and then return true according to this description?
So undefined really means undefined. Not False, not True, not 0, not empty string. So when you compare undefined to anything, the result is always false, it is not equal to that. undefined == null is true.
The Boolean value of undefined is false. The value of Not only undefined but also null, false, NaN, empty string is also false.
undefined does not equal undefined.
"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. A String
in javascript can have an undefined value, but here the String
object has a defined value that just happens to spell "undefined".
Using the explanation you've provided, the undefined
value on the right side would be converted to a String
object with no value assigned, and then compared to the String
"undefined", failing the comparison.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With