Home >Web Front-end >JS Tutorial >Are various types of variables in js true or false in if conditions_javascript tips
The weak typing of js makes many things confusing. For example, whether a variable is true or false in an if condition. If a non-boolean type variable is placed in an if condition in a strongly typed language, it is necessary. Type conversion is required, but js does not need it. Let’s test it below to test the performance of common variable types in if conditions
!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) } }()
Set various variable types and put them into if conditions respectively
Execution results
a=undefined
d=0
e=null
f=undefined
g=
h=
i=false