What is passed as true (or false) in the if expression for the variable

In this code, what values ​​of myVar will behave as true and false ?

 if(myVar){} 

In JavaScript, for example, the following values ​​are false

 null undefined 0 '' false 
+4
source share
2 answers

An object

false if the instance is null ; true otherwise

Line

false if null or the empty string "" ; true otherwise

Number, int or uint

false if the value is NaN or 0 ; true otherwise

zero

false

From here .

+11
source

If (myVar) is a bool, then it will become apparent if it is true, and will not work if not. If bool has never been initialized (for example, var myBool:Boolean; instead of var myBool:Boolean = true ), then by default it is erroneous. The same concept applies to embedded objects such as Number, int, etc. To a large extent, for everything else, it will pass only as true if the object was initialized through the constructor of the object or through a direct assignment, for example:

 var a:MovieClip = new MovieClip(); var b:MovieClip = a; 
+1
source

All Articles