How can I determine if a variable is 'undefined' or 'null'?

前端 未结 30 1521
耶瑟儿~
耶瑟儿~ 2020-11-22 03:30

How do I determine if variable is undefined or null?

My code is as follows:

var EmpN         


        
30条回答
  •  失恋的感觉
    2020-11-22 04:13

    if you create a function to check it:

    export function isEmpty (v) {
     if (typeof v === "undefined") {
       return true;
     }
     if (v === null) {
       return true;
     }
     if (typeof v === "object" && Object.keys(v).length === 0) {
       return true;
     }
    
     if (Array.isArray(v) && v.length === 0) {
       return true;
     }
    
     if (typeof v === "string" && v.trim().length === 0) {
       return true;
     }
    
    return false;
    }
    

提交回复
热议问题