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

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

    I've come to write my own function for this. JavaScript is weird.

    It is usable on literally anything. (Note that this also checks if the variable contains any usable values. But since this information is usually also needed, I think it's worth posting). Please consider leaving a note.

    function empty(v) {
        let type = typeof v;
        if (type === 'undefined') {
            return true;
        }
        if (type === 'boolean') {
            return !v;
        }
        if (v === null) {
            return true;
        }
        if (v === undefined) {
            return true;
        }
        if (v instanceof Array) {
            if (v.length < 1) {
                return true;
            }
        } else if (type === 'string') {
            if (v.length < 1) {
                return true;
            }
            if (v === '0') {
                return true;
            }
        } else if (type === 'object') {
            if (Object.keys(v).length < 1) {
                return true;
            }
        } else if (type === 'number') {
            if (v === 0) {
                return true;
            }
        }
        return false;
    }
    

    TypeScript-compatible.


    This function should do exactly the same thing like PHP's empty() function (see RETURN VALUES)

    Considers undefined, null, false, 0, 0.0, "0" {}, [] as empty.

    "0.0", NaN, " ", true are considered non-empty.

    0 讨论(0)
  • The shortest and easiest:

    if(!EmpName ){
     // DO SOMETHING
    }
    

    this will evaluate true if EmpName is:

    • null
    • undefined
    • NaN
    • empty
    • string ("")
    • 0
    • false
    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-11-22 04:17

    With the solution below:

    const getType = (val) => typeof val === 'undefined' || !val ? null : typeof val;
    const isDeepEqual = (a, b) => getType(a) === getType(b);
    
    console.log(isDeepEqual(1, 1)); // true
    console.log(isDeepEqual(null, null)); // true
    console.log(isDeepEqual([], [])); // true
    console.log(isDeepEqual(1, "1")); // false
    etc...
    

    I'm able to check for the following:

    • null
    • undefined
    • NaN
    • empty
    • string ("")
    • 0
    • false
    0 讨论(0)
  • 2020-11-22 04:17

    I still think the best/safe way to test these two conditions is to cast the value to a string:

    var EmpName = $("div#esd-names div#name").attr('class');
    
    // Undefined check
    if (Object.prototype.toString.call(EmpName) === '[object Undefined]'){
        // Do something with your code
    }
    
    // Nullcheck
    if (Object.prototype.toString.call(EmpName) === '[object Null]'){
        // Do something with your code
    }
    
    0 讨论(0)
  • 2020-11-22 04:18

    Combining the above answers, it seems the most complete answer would be:

    if( typeof variable === 'undefined' || variable === null ){
        // Do stuff
    }
    

    This should work for any variable that is either undeclared or declared and explicitly set to null or undefined. The boolean expression should evaluate to false for any declared variable that has an actual non-null value.

    0 讨论(0)
提交回复
热议问题