当前位置:网站首页>Example of if directly judging data type in JS

Example of if directly judging data type in JS

2022-06-23 15:59:00 Douqu programming

if() It is generally used to judge the Boolean value of the calculation result , Such as ===,==,>,>=,<,<= etc. ; Or judge Function return value after forced conversion to Boolean value, etc ;

Automatically execute when judging Boolean() Method , Trying to convert the result to a Boolean value

But sometimes, when judging the data type directly , The following situations are Boolean() Results of transformation

1. Judged as false (false)

//var flag=undefined;
//var flag=null;
//var flag=0;
//var flag=NaN;
//var flag="";
//var flag;
//var flag=false;

if(!flag){
	alert(" Execute this logic ")
}

2. A situation judged to be true (true)

//var flag={};  // An empty object 
//var flag=[];
//var flag=1;   // except 0 and NaN All numbers except 
//var flag=true;
//var flag=function(){};
//var flag=" ";  // A string with only spaces 
//var flag=/ /;  // Regular expressions 
if(flag){
	alert(" Execute this logic ")
}

 

原网站

版权声明
本文为[Douqu programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231526552713.html