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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM