The case
var resu;
undefined? resu = "esto seria inesperado" : resu = "debería dar esto";
console.log(resu);
But when I go to confirm the theory that undefined
is false
:
var resu;
undefined == false? resu = "deberia dar esto" : resu = "esto seria inesperado";
console.log(resu);
That's because it
undefined
is not false , instead the boolean representation ofundefined
if is.To understand what is happening in your code, the first thing to keep in mind is that a ternary operator does a Boolean evaluation of the part to the left of the operator (before the sign
?
).In this case or similar:
A boolean evaluation of the part to the left (
undefined
) will be done, as specified in section 7.1.2 of ECMA Script . Whenundefined
converted to boolean, the result of that conversion isfalse
.Therefore, the expression takes the rightmost value, because the conversion from
undefined
to boolean is equal tofalse
.Here on the contrary:
what is specified in the abstract equality comparison algorithm is applied , documented in section 7.2.13 Abstract Equality Comparison of ECMA Script. We would be facing case 7 of said algorithm, which says: if the type of
y
(value on the right) is boolean, it returns the result of the comparisonx == ToNumber(y)
. In other words: convertfalse
to number and compare if that result is equal toundefined
. If we convertfalse
to a number, we get a0
, and the evaluationundefined==0
gives as a resultfalse
and therefore the expression takes the value of the right end.Note that if you force a conversion of
undefined
and ask if it is equal tofalse
, then yes the ternary operator would evaluate totrue
, because it would be the same as askingfalse==false
:Even if you do a strict comparison (of type and value), the result would be
true
:Look at these two examples and draw your own conclusions:
Here is a code snippet with examples. For clarity I have used variables, I have printed their type. You will see everything said above and you will see that it is
undefined
never equal tofalse
, what is equal tofalse
would be an eventual conversion fromundefined
to boolean.Touching on the subject of boolean data, we know that its logical representation is as follows:
True which in binary representation is equal to 1
False which in binary representation is equal to 0
So it is clear that the boolean data type has a predefined structure and a specific value assigned by default in javascript
What happens with undefined?
It is also a data type ( special primitive ), however, it does not have a value assigned by default in the language. It is used to specify or reserve a space in memory to which no value has been assigned.
It is because of that
console.log(undefined == false)
does not return trueFalse can be taken as
0
, however, this is not the case with undefined because it does not have a numerical representation in terms of assigned value, hence the fact that it is a special primitive because it generates certain exceptions in the standard behavior of the language. I could declare a variable:Even when it exists
x
, it has a space reserved in memory and you could do operations with it later... what happens with the previous instruction is that it reserves the space in memory forx
but it is not defined (it is undefined), that is, it has no representation logic and that is why:console.log(x == false)
won't return true eitherOf course javascript's type conversion handles these kinds of exceptions if you do a strict logical type comparison:
console.log(Boolean(undefined) == false)
if it will return trueIt will return true because you are forcing undefined to take a logical or boolean value and the default behavior when declaring it a boolean will always be false unless you indicate otherwise.
For more information you can visit: undefined-javacrypt you can also read null vs undefined
Precisely because the purpose of undefined is to be able to declare a variable (for example) without necessarily knowing what its type is. This is why javascript handles this kind of exception to its indirect type conversion rule.
It is different to compare the boolean of undefined with false:
With the above statement you are forcing an automatic type conversion directly so javascript by default "assigns" the value false in the conversion and
false==false
returns true .I hope to be helpful. All the best!
The issue is that
undefined
andfalse
(ortrue
) are of different types:So you see, why would two values of different types be the same?
undefined
andboolean
are two of JavaScript's primitive types.If you want the boolean representation of
undefined
you can do what @Iker says in his answer. Another way is to use double negation to convertundefined
to its boolean representation: