Why is "undefined" == undefined incorrect?

The operator is ==really funny. He usually does not behave as he thinks.

This led me to find out exactly what happens below the tip of the iceberg, and according to MDN it looks like this:

If the two operands are not of the same type, JavaScript converts the operands then apply strict comparison. If either the operand is a number or a boolean, the operands, if possible, are converted to numbers; even if any operand is a string, the other operand is converted to if possible. If both operands are objects, then JavaScript compares internal references, which are equal when the operands refer to the same object in memory.

(source)

So why "undefined" == undefinednot evaluate true?

Should I not convert undefinedto "undefined"and return true as described?

+5
source share
5 answers

"undefined"has the meaning. These are 9 letters: undefined. Therefore, the string "undefined" does not matter undefined. A Stringin javascript may have the value undefined, but here the object Stringhas a specific value, which just happens with the "undefined" spell.

Using the explanation provided, the value undefinedon the right side will be converted to an object Stringwithout an assigned value, and then compared to String"undefined", otherwise a comparison.

+20

: ECMAScript 5.1. 11.9.3 , :

  • (x) (y), //
  • x null, y - undefined, true.
  • x undefined, y - null, true.
  • Type (x) - Number Type (y) - String, x == ToNumber (y).
  • Type (x) - String Type (y) - Number, ToNumber (x) == y.
  • Type (x) , ToNumber (x) == y.
  • (y) , x == ToNumber (y).
  • Type (x) String, Number Type (y) - Object, x == ToPrimitive (y).
  • Type (x) Object Type (y) , , ToPrimitive (x) == y.
  • false.

Type (x) string, Type (y) - undefined, , .

+2

undefined javascript undefined. , x == undefined, javascript: , .

0

"undefined"- literally string, and undefined- object. I think it translates the right operand with nothingor ""or an empty string. So "undefined" == nullreturn false.

0
source

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

var a = undefined;

//if( a == undefined ) 

//if( a === undefined )
-2
source

All Articles