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

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

提交回复
热议问题