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

前端 未结 30 1527
耶瑟儿~
耶瑟儿~ 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:17

    Since you are using jQuery, you can determine whether a variable is undefined or its value is null by using a single function.

    var s; // undefined
    jQuery.isEmptyObject(s); // will return true;
    
    s = null; // defined as null
    jQuery.isEmptyObject(s); // will return true;
    
    // usage
    if(jQuery.isEmptyObject(s)){
        alert('Either variable: s is undefined or its value is null');
    }else{
         alert('variable: s has value ' + s);
    }
    
    s = 'something'; // defined with some value
    jQuery.isEmptyObject(s); // will return false;
    

提交回复
热议问题