一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

js判断数组是否为空程序代码

时间:2013-05-08 编辑:简简单单 来源:一聚教程网

例1

 代码如下 复制代码

if(tt==null || tt==""){ 
    alert("kong"); 
} else { 
    alert("bukong"); 
}

例2

 代码如下 复制代码

var is = function (obj,type) {
return (type === "Null" && obj === null) ||
(type === "Undefined" && obj === void 0 ) ||
(type === "Number" && isFinite(obj)) ||
Object.prototype.toString.call(obj).slice(8,-1) === type;
}

数组某个元素是否为空
1. 如果是数组元素不存在: if (arr[x] == undefined) 判断
2. 如果是元素里的内容为空值,那就需要根据你填充数组的值来做判断。

 代码如下 复制代码

var b = new Array (1,null,'')
console.log(b[1] == null); //填充值为null的元素
console.log(b[2] == ''); //填充值为空('')的元素
console.log(b[3] == undefined); //判断不存在的元素

热门栏目