Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "undefined" == undefined isn't true?

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?

like image 481
ajax333221 Avatar asked May 21 '12 20:05

ajax333221


People also ask

Why undefined == false is false?

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.

Does undefined == false?

The Boolean value of undefined is false. The value of Not only undefined but also null, false, NaN, empty string is also false.

Does undefined equal undefined?

undefined does not equal undefined.


1 Answers

"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.

like image 133
Jonathan M Avatar answered Nov 15 '22 01:11

Jonathan M