4

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?

5
  • 5
    The key words in there are "if possible"
    – Pointy
    Commented May 21, 2012 at 20:04
  • 1
    @Pointy but it is very possible typeof ("" + undefined)
    – ajax333221
    Commented May 21, 2012 at 20:05
  • 3
    I guess the conversion is undefined => "" and the comparison then is "undefined" vs. ""
    – Pekka
    Commented May 21, 2012 at 20:05
  • Obligatory: twitter.com/#!/lassekorsgaard/status/201772655223902210
    – David
    Commented May 21, 2012 at 20:09
  • The conversion rules for + and for == are different. Read starting at page 80 in the 5.1 spec.
    – Pointy
    Commented May 21, 2012 at 20:09

5 Answers 5

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.

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.

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

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.

1
  • undefined is not an object. It's of type Undefined, not of type Object.
    – jmbucknall
    Commented May 21, 2012 at 20:37
-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 )
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.