The weak type of js makes people feel that many things are confusing, such as whether a variable is true or false in an if condition. If a variable of non-boolean type is placed in an if condition in a strongly typed language, type conversion is required, but js does not need it. Let's test it below to test the performance of common variable types in an 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 placed in if conditions respectively
Execution results
a=undefined
d=0
e=null
f=undefined
g=
h=
i=false