Javascript function true / false

I'm new to JavaScript and I just discovered strange behavior that I can't figure out:

var magicVar = Math.sin;
magicVar == true; // it returns false
magicVar === true; // it returns false
if (magicVar) console.log("???"); // it prints "???"

      

What's happening?

Thank.

+3


source to share


5 answers


var magicVar = Math.sin;

      

Here magicVar

is a link to the function Math.sin

that actually is Object

(see Function on MDN)

The Function constructor creates a new Function object. In JavaScript, every function is actually a Function object.


magicVar == true; // it returns false 

      

This false

: from Equality and Similarity Comparison on MDN, if you compare a type operand Object

with a type operand Boolean

using an operator ==

you always get false

(look at Free Equality using table == ).

[EDIT] As Bergie pointed out in the comments, in some cases you might in some cases have objects that return true when compared to a boolean.

What actually happens behind the scenes is that the comparison algorithm described in ES6 ยง7.2.12 is applied .

7.2.12 Abstract equality comparison

The comparison x == y, where x and y are values, evaluates to true or false. This comparison is done as follows:

  • ReturnIfAbrupt (x).
  • ReturnIfAbrupt (y).
  • If type (x) is the same as type (y), then return the result of performing a strict coefficient comparison x === y.
  • If x is null and y is undefined, return true.
  • If x is undefined and y is null, return true.
  • If Type (x) is Number and Type (y) is String, return the result of the comparison x == ToNumber (y).
  • If Type (x) is String and Type (y) is Number, return the result of the comparison ToNumber (x) == y.
  • If Type (x) is Boolean, return the result of the comparison ToNumber (x) == y.
  • If type (y) is boolean, return the result of the comparison x == ToNumber (y).
  • If Type (x) is either String, Number, or Symbol, and Type (y) is Object, then return the result of the comparison x == ToPrimitive (y).
  • If Type (x) is Object and Type (y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive (x) == y.
  • Returns false.

In your case, the following happens:

magicVar == true
magicVar == Number(true) // Step 9. x == ToNumber(y).
magicVar == 1
toPrimitive(magicVar) == 1 // Step 11. ToPrimitive(x) == y.
magicVar.toString() == 1
"function sin() { [native code] }" == 1
Number("function sin() { [native code] }") == 1 // Step 7. ToNumber(x) == y.
NaN === 1 // Step 3. x === y.
false

      



But comparison, for example, with an object like:

{
     valueOf: function(){
         return 1;
     }
}

      

You'll get:

true == {valueOf(){return 1;}} // it returns true

      


magicVar === true; // it returns false

      

This is trivial false

, the types of the operands are different, and the operator ===

checks if the operands have the same types and values.


if (magicVar) console.log("???"); // it prints "???"

      

As all the other answers said magicVar

here, in context Boolean

and are forced totrue

+4


source


magicVar

true as value . This is true in the sense that it is not empty, it is not null or undefined. Thus, any non-zero value will be a true value. But does this value equal true? Not. This is true because boolean is not true as value.

Summarizing:



magicVar == true || magicVar === true; // returns false
Boolean(magicVar); // returns true

      

+2


source


The concept of understanding here is to distinguish between true and false versus true and false boolean values. See here for the truth and here for the fake

In short, the condition if

passes if the expression is true and magicVar

is function

(true).

+1


source


magicVar is not true. It is equal to the Math.sin function.

So, in the if statement, it evaluates to the truthful value (it is defined and does not evaluate to the falsity value).

http://www.sitepoint.com/javascript-truthy-falsy/

0


source


Comparison function Math.sin

with the boolean

constant return false

, since these variables are of different types. But when you put a variable not-bool in the condition if

: for example if (Math.sin)...

or if (window)...

, it returns true if the variable is not zero.

0


source







All Articles