5

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?

Community
  • 1
  • 1
ajax333221
  • 11,436
  • 16
  • 61
  • 95

5 Answers5

20

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

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
2

For these kinds of questions, it's worth going back to the source: the ECMAScript 5.1 specification. Section 11.9.3 has the complete description of what happens with double-equals:

  1. If Type(x) is the same as Type(y), then // some rules
  2. If x is null and y is undefined, return true.
  3. If x is undefined and y is null, return true.
  4. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
  5. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
  6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
  7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
  8. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
  9. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
  10. Return false.

Since in this case Type(x) is string and Type(y) is undefined, the rules state that the result should be false.

jmbucknall
  • 2,061
  • 13
  • 14
0

undefined is a primitive built in value in javascript of built in type Undefined. And doing a comparison such as x == undefined, has a special meaning in javascript: It checks whether a variable is not defined yet.

Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
0

"undefined" is literally a string and undefined is an object. I think it casts right side operand with nothing or "" or an empty string. So "undefined" == null return false.

Vishal
  • 2,161
  • 14
  • 25
-2

undefined is a special javascript keyword, the following are ways to check for undefined variables/properties.

var a = undefined;

//if( a == undefined ) 

//if( a === undefined )
Steve Binder
  • 2,270
  • 1
  • 14
  • 6