JavaScript空判断

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hongweigg/article/details/82733942

在JavaScript中,空判断比较常见,但究竟应该如何正确地使用空判断呢?不同的数据类型有不同的判断方法,不能同一而论,在判断空前,应先确定数据的类型。

1、不同类型具有不同的判空方法

在判空前应预判数据的类型,如果期望类型不清晰,则可能会导致错误的判断或考虑情况不周全。

序号 测试类型 测试代码 结果
1 无参数或传入undefined test1(); 输入值:undefined,类型:undefined,返回值:false
test1(undefined, 'undefined'); 输入值:undefined,类型:undefined,返回值:false
2 数字 test1(0, '数字0'); 输入值:数字0,类型:number,返回值:false
test1(-1, '数字-1'); 输入值:数字-1,类型:number,返回值:true
test1(NaN, '数字NaN'); 输入值:数字NaN,类型:number,返回值:false
3 字符串 test1('', '字符串(无内容)'); 输入值:字符串(无内容),类型:string,返回值:false
test1('0', '字符串0'); 输入值:字符串0,类型:string,返回值:true
test1(' ','字符串空格'); 输入值:字符串空格,类型:string,返回值:true
4 对象 test1({},'空对象({})'); 输入值:空对象({}),类型:object,返回值:true
test1([],'空数组'); 输入值:空数组,类型:object,返回值:true
test1(null, 'null'); 输入值:null,类型:object,返回值:false

 2、常用判空函数:

序号 测试类型 函数 测试代码 结果
1 字符串判空 /**
*字符串去除所有空格
*/
function trim(a){
  if(typeof a =='string'){
    return a.replace(/\s+/,'');
  }else {
    return a;
  }
}


/**
*字符串判空
*/
function isEmpty(a){
  var b = trim(a);
  if((typeof a) == 'string'  && b){
    return false;
   }else {
      return true;
   }
}
println(isEmpty(' '));
println(isEmpty());
println(isEmpty(''));
println(isEmpty(' 1 '));
true
true
true
false
2 数字判空 /**
*数字判空,如果不是数字类型,则应首先进行类型转换
*/
function isNull(a){
  if((typeof a) == 'number' && a !=NaN){
    return false;
  }else {
    return true;
  }
}
println(isNull());
println(isNull(null));
println(isNull(''));
println(isNull(' '));
println(isNull('9'));
println(isNull([1]));
true
true
true
true
true
true
3 综合判空,不考虑为对象的情况 /**
* 不分类型,不考虑传入值为对象的情况
*/
function isNullEmpty(a){
  if((typeof a) == 'string'){
    return isEmpty(a);
  } else if((typeof a) == 'number' && a !=NaN){
    return false;
  }else {
    return true;
  }
}
println(isNullEmpty());
println(isNullEmpty(null));
println(isNullEmpty(''));
println(isNullEmpty(' '));
println(isNullEmpty('9'));
println(isNullEmpty([1]));
true
true
true
true
false
true
4 对象判空 /**
*对象判空
*/
function isEmptyObj(a){
  if((typeof a) == 'object' && a!=null){
    return false;
  }else {
    return true;
  }
}
println(isEmptyObj(null));
println(isEmptyObj());
println(isEmptyObj(undefined));
true
true
true
5 数组判空 /**
*数组判空,数组是对象的一种
*/
function isEmptyArray(a){

  if((typeof a) == 'object' && a!=null && a.length>0){
  return false;
  }else {
    return true;
  }
}
println(isEmptyArray(null));
println(isEmptyArray([]));
println(isEmptyArray([1,'2',{test:'ss'}]));
true
true
false

3、测试代码

	<script lang="javascript">
     function println(str){
    		document.writeln(str + '<br>');
    } 
    
	
	
	function test1(a,desc){
		var str = '输入值:' + desc + ',类型:'+ (typeof a);
	
		if(a){
			println(str + ',返回值:<span style="color:#ff0000;">true</span>');
		}else {
			println(str + ',返回值:<span style="color:#000000;">false</span>');
		} 
	}
	
	/**
	*字符串去除所有空格
	*/
	function trim(a){
		if(typeof a =='string'){
			return a.replace(/\s+/,'');
		}else {
			return a;
		} 
	}
	
	
	/**
	*字符串判空
	*/
	function isEmpty(a){
		var b = trim(a);
		
		if((typeof a) == 'string' && b){
			return false;
		}else {
			return true;
		} 
	}
	
	/**
	*数字判空,如果不是数字类型,则应首先进行类型转换
	*/
	function isNull(a){
 
		if((typeof a) == 'number' && a !=NaN){ 
			return false;
		}else {
			return true;
		} 
	}
	
	
	/**
	*数组判空,数组是对象的一种
	*/
	function isEmptyArray(a){
 
		if((typeof a) == 'object' && a!=null && a.length>0){ 
			return false;
		}else {
			return true;
		} 
	}
	
	
	/**
	*对象判空
	*/
	function isEmptyObj(a){
		if((typeof a) == 'object' && a!=null){ 
			return false;
		}else {
			return true;
		} 
	}
	
	
	/**
	* 不分类型,不考虑传入值为对象的情况
	*/
	function isNullEmpty(a){
 
		if((typeof a) == 'string'){
			return isEmpty(a);
		} else if((typeof a) == 'number' && a !=NaN){
			return false;
		}else {
			return true;
		} 
	}	
	
	
	println('-------- 无参数或传入undefined--------------');
	test1();
	test1(undefined, 'undefined');
	
	println('------------------数字----------------------');		
	test1(0, '数字0');
	test1(-1, '数字-1');
	test1(NaN, '数字NaN');
	
	println('-------------------字符串-------------------');
	test1('', '字符串(无内容)');
	test1('0', '字符串0');
	test1(' ','字符串空格');
	
	println('-------------------对象--------------------');	
	test1({},'空对象({})');
	test1([],'空数组');
	test1(null, 'null');	
	
	println('-----------------字符串判空----------------');	
	println(isEmpty('   '));
	println(isEmpty());
	println(isEmpty(''));
	println(isEmpty('  1   '));

	println('----------------数字判空-------------------');	
	println(isNull());
	println(isNull(null));
	println(isNull(''));
	println(isNull(' '));
	println(isNull('9'));
	println(isNull([1]));	
	
	
	println('----------------对象判空--------------------');	
	println(isEmptyObj(null));
	println(isEmptyObj());
	println(isEmptyObj(undefined));			
	
	
	println('----------------综合判空,不考虑为对象的情况--------------------');		
	println(isNullEmpty());
	println(isNullEmpty(null));
	println(isNullEmpty(''));
	println(isNullEmpty(' '));
	println(isNullEmpty('9'));
	println(isNullEmpty([1]));		
	
	
	println('----------------数组判空--------------------');	
	println(isEmptyArray(null));
	println(isEmptyArray([]));
	println(isEmptyArray([1,'2',{test:'ss'}]));


	
	println('----------------数组元素类型可不统一--------------------');	
	println([1,'2',{test:'ss'}]);
	
	</script>

猜你喜欢

转载自blog.csdn.net/hongweigg/article/details/82733942