because the Boolean value of undefined is false, but when compared to false it is different.

6

The case

var resu;

undefined?  resu = "esto seria inesperado" : resu = "debería dar esto";
   
console.log(resu);

But when I'm going to confirm the theory that undefined is false :

var resu;

undefined == false? resu = "deberia dar esto" : resu = "esto seria inesperado";

console.log(resu);
    
asked by Diego Felipe Orozco Penagos 25.06.2018 в 20:59
source

4 answers

11

That's because undefined is not false , instead the boolean representation of undefined is.

console.log(undefined == false)

console.log(Boolean(undefined) == false)

console.log(new Boolean(undefined) == false)

console.log(new Boolean(undefined) == true)
    
answered by 25.06.2018 в 21:02
3

To understand what happens in your code, the first thing to keep in mind is that a ternary operator makes a Boolean evaluation of the part that is to the left of the operator (before the sign ? ).

In this case or similar:

undefined ? "esto seria inesperado" : "debería dar esto";

A Boolean evaluation of the part on the left ( undefined ) will be done, as specified in the section 7.1.2 of ECMA Script . When undefined is converted to boolean, the result of that conversion is false .

Therefore, the expression takes the value of the far right, because the conversion of undefined to Boolean is equal to false .

Here, on the contrary:

undefined == false ? "deberia dar esto" : "esto seria inesperado";

the one specified in the abstract equality comparison algorithm applies, documented in the section 7.2.13 Abstract Equality Comparison from ECMA Script. We would be before the case 7 of said algorithm, which says: if the type of y (value on the right) is Boolean, returns the result of the comparison x == ToNumber(y) In other words: convert false to number and compare if that result equals undefined . If we convert false to number, we get a 0 , and the evaluation undefined==0 results in false and therefore the expression takes the value of the far right.

Notice that if you force a conversion of undefined and questions if it equals false , then yes the ternary operator would evaluate as true , because it would be the same as asking false==false :

Boolean(undefined)==false ? "esto seria inesperado" : "debería dar esto";

Even if you make a strict comparison (of type and value), the result would be true :

Boolean(undefined)===false ? "esto seria inesperado" : "debería dar esto";

Look at these two examples and draw your own conclusions:

Boolean(undefined)==0  ? "esto seria inesperado" : "debería dar esto";
Boolean(undefined)===0 ? "esto seria inesperado" : "debería dar esto";

Here I leave a fragment of code with examples. For more clarity I have used variables, I have printed their type. You will see everything said above and you will see that undefined never is equal to false , which is equal to false would be a possible conversion of undefined to boolean.

var status;
var myUndefined = undefined;
var myBooleanFromUndefined= Boolean(undefined);
console.log("La variable myUndefined es de tipo: "+typeof myUndefined +" \ny su valor es: "+myUndefined);
console.log("La variable myBooleanFromUndefined es de tipo: "+typeof myBooleanFromUndefined +" \ny su valor es: "+myBooleanFromUndefined);


status = myUndefined ? "TRUE: esto seria inesperado" : "FALSE: debería dar esto";
console.log(status);



status = myUndefined == false ? "TRUE: deberia dar esto" : "FALSE: esto seria inesperado";
console.log(status);

status = myBooleanFromUndefined == false ? "*TRUE: deberia dar esto" : "FALSE: esto seria inesperado";
console.log(status);

status = myBooleanFromUndefined === false ? "*TRUE: deberia dar esto" : "FALSE: esto seria inesperado";
console.log(status);

status = myBooleanFromUndefined == 0 ? "*TRUE: deberia dar esto" : "FALSE: esto seria inesperado";
console.log(status);

status = myBooleanFromUndefined === 0 ? "TRUE: deberia dar esto" : "*FALSE: esto seria inesperado";
console.log(status);

status = myUndefined == undefined ? "TRUE: deberia dar esto" : "FALSE: esto seria inesperado";
console.log(status);
    
answered by 25.06.2018 в 23:27
2

The issue is that undefined and false (or true ) are of different types:

> typeof undefined
"undefined"
> typeof false
"boolean"
> undefined == false
false

So you see, why would two values of different types be the same? undefined and boolean are two of the primitive types of JavaScript.

If you want the boolean representation of undefined you can do what @Iker says in your answer. Another way is to use double negation to convert undefined to its Boolean representation:

> !undefined
true
> !!undefined
false
> !!undefined == false
true
    
answered by 25.06.2018 в 21:11
2
  

Why the Boolean value of undefined is false, but when   compare with false da they are different?

Touching the boolean data topic we know that its logical representation is as follows:

True that in binary representation is equal to 1

False that 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 a data type as well ( special primitive ), however, has no value assigned by default in the language. It is used to specify or reserve a memory space to which no value has been assigned.

That's why

console.log(undefined == false) does not return true

False can be taken as 0 , however, it is not so with undefined because it does not have numerical representation in terms of assigned value, hence the fact that make it a special primitive because it generates certain exceptions in the standard behavior of the language. I could declare a variable:

var x;

Even though there is x , it has a reserved space in memory and could do operations with it later ... what happens with the previous instruction is that it reserves the space in memory for x but it is not defined (it is undefined), that is, it does not have logical representation and that is why:

console.log(x == false) will also not throw true

Of course, the conversion of javascript types handles this type of exception if you make a strict comparison of logical type:

console.log(Boolean(undefined) == false) if it will throw true

It will throw 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 that it is not.

For more information you can visit: undefined-javacript you can also read < a href="https://fernetjs.com/2011/12/null-vs-undefined/"> null vs undefined

  

Why if the equality operator tries to convert the types, with   undefined does not the same thing happen?

Precisely because the goal of undefined is to declare a variable (for example) without necessarily knowing what its type is. That is why javascript handles this kind of exception to its indirect type conversion rule.

Different is to compare the boolean of undefined with false:

console.log(Boolean(undefined) == false)

With the previous instruction you are forcing an automatic type conversion directly so javascript by default "assigns" the value of false in the conversion and false==false as result is true . p>

I hope it helps. Greetings!

    
answered by 26.06.2018 в 16:32