七度&光 2010-04-15 18:12 采纳率: 22.2%
浏览 588
已采纳

如何确定变量是'undefined'还是'null'?

How do I determine if variable is undefined or null? My code is as follows:

var EmpName = $("div#esd-names div#name").attr('class');
if(EmpName == 'undefined'){
  //DO SOMETHING
};
<div id="esd-names">
  <div id="name"></div>
</div>

But if I do this, the JavaScript interpreter halts execution.

转载于:https://stackoverflow.com/questions/2647867/how-to-determine-if-variable-is-undefined-or-null

  • 写回答

25条回答 默认 最新

  • python小菜 2010-04-15 18:14
    关注

    You can use the qualities of the abstract equality operator to do this:

    if (variable == null){
        // your code here.
    }
    

    Because null == undefined is true, the above code will catch both null and undefined.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(24条)

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题