js 判断数据是否为空

原文:https://www.cnblogs.com/ooo0/p/6511723.html

//    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("为空");
    }

 

posted @ 2019-06-13 11:47  鳳舞九天  阅读(55620)  评论(1编辑  收藏  举报