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

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

提交回复
热议问题