在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、常用判空函数:
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>
js 判空
js判断是否为空的代码如下:
// var a = "";
// var a = " ";
// var a = null;
// var a = undefined;
// var a = [];
// var a = {};
// var a = NaN;
if(a === undefined) { // 只能用 === 运算来测试某个值是否是未定义的
console.log("为undefined");
}
if(a == null) { // 等同于 a === undefined || a === null
console.log("为null");
}
// String
if(a == "" || a == null || a == undefined){ // "",null,undefined
console.log("为空");
}
if(!a){ // "",null,undefined,NaN
console.log("为空");
}
if(!$.trim(a)){ // "",null,undefined
console.log("为空");
}
// Array
if(a.length == 0){ // "",[]
console.log("为空");
}
if(!a.length){ // "",[]
console.log("为空");
}
// Object {}
if($.isEmptyObject(a)){ // 普通对象使用 for...in 判断,有 key 即为 false
console.log("为空");
}
JavaScript
程序是由若干语句组成的,语句是编写程序的指令。JavaScript
提供了完整的基本编程语句,它们是:
赋值语句、
switch选择语句、
while循环语句、
for循环语句、
for each循环语句、
do...while循环语句、
break循环中止语句、
continue循环中断语句、
with语句、
try…catch语句、
if语句(if..else,if…else if…)。