js 判斷數據是否爲空

 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("爲空");
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章