如何确定变量是否undefined或为null ?我的代码如下: 
var EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
  //DO SOMETHING
};<div id="esd-names">
  <div id="name"></div>
</div>但是如果我这样做,JavaScript 解释器会暂停执行。
您可以使用抽象相等运算符的质量来执行此操作:
if (variable == null){
    // your code here.
}因为null == undefined为 true,所以上面的代码将捕获null和undefined 。 
同时捕获null和undefined的标准方法是: 
if (variable == null) {
     // do something 
}- 这是 100%相当于更明确但不简洁:
if (variable === undefined || variable === null) {
     // do something 
}在编写专业 JS 时,理所当然地认为类型相等和== vs ===的行为是理解的。因此我们使用==并且仅与null进行比较。 
建议使用typeof的评论完全错误。是的,如果变量不存在,我上面的解决方案将导致 ReferenceError。 这是件好事。这个 ReferenceError 是可取的:它可以帮助您在发布代码之前找到错误并修复它们,就像其他语言中的编译器错误一样。 
您不应该在代码中引用任何未声明的变量。
if (variable == null) {
    // Do stuff, will only match null or undefined, this won't match false
}