如何确定变量是“未定义”还是“空”? [英] How can I determine if a variable is 'undefined' or 'null'?
本文介绍了如何确定变量是“未定义”还是“空”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何确定变量是未定义
还是 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解释器将停止执行。 / p>
But if I do this, the JavaScript interpreter halts execution.
推荐答案
您可以使用抽象均等运算符可以这样做:
You can use the qualities of the abstract equality operator to do this:
if (variable == null){
// your code here.
}
因为 null == undefined
为真,上面的代码将同时捕获 null
和 undefined
。
Because null == undefined
is true, the above code will catch both null
and undefined
.
这篇关于如何确定变量是“未定义”还是“空”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文