js 判断是否为空

 我来答
没钱的萌娃纸
高粉答主

推荐于2019-08-28 · 繁杂信息太多,你要学会辨别
知道小有建树答主
回答量:230
采纳率:100%
帮助的人:6万
展开全部

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…)。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式