The various types of variables in js are true or false in the if condition

  • 2020-03-30 03:31:22
  • OfStack

Js weak to make people feel a lot of things are confused, for instance variables in the if condition is true or false, if in a strongly typed language, a non Boolean variables of type on the if condition, is the need for type conversion, but js don't need to test below, testing common variable type's performance in the if condition


!function test1(){ 

<span style="color:#ff0000;">var a,b=-1,c= 1,d= 0,e=null,f=undefined,g='',h="";</span> 
if(!a){ 
console.log('a='+a) 
} 
if(!b){ 
console.log("b="+b) 
} 
if(!c){ 
console.log("c="+c) 
} 
if(!d){ 
console.log("d="+d) 
} 
if(!e){ 
console.log("e="+e) 
} 
if(!f){ 
console.log("f="+f) 
} 
if(!g){ 
console.log("g="+g) 
} 
if(!h){ 
console.log("h="+h) 
} 

}() 

Various variable types are set and put into the if condition

The execution result
A = undefined
D = 0
E = null
F = undefined
G =
H =
I = false


Related articles: