js中对undefined,null等的判断

借鉴: 
https://www.cnblogs.com/xinggood/p/6568624.html

 

对js中不同数据的布尔值类型总结:false:空字符串;null;undefined;0;NaN。
true:除了上面的false的情况其他都为true;

 

实例:

对于单个对象,字符串等适合方式:

if(user.office){
$("#eamCode").attr("value", user.office.eamCode);
}
如果user.office返回的是null 或者undefined 或者是空字符串。都会是false;

 

对于数组、字符串。可以使用length ,

var project =  user.userProject ;
if(project.length>0){
使用长度判断。

 

个人的浅见。有不对的请指正!

 

posted @ 2019-03-13 10:50  办公室的仙人掌  阅读(345)  评论(0编辑  收藏  举报