如何确定变量是“未定义的”还是“空的”?如何确定变量是否是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解释器就会停止执行。
3 回答
绝地无双
TA贡献1946条经验 获得超4个赞
nullundefined
if (variable == null) {
// do something }if (variable === undefined || variable === null) {
// do something }=======null.
再编辑
typeof
在代码中不应该有任何对未声明变量的引用。
慕标琳琳
TA贡献1830条经验 获得超9个赞
if( typeof variable === 'undefined' || variable === null ){
// Do stuff}添加回答
举报
0/150
提交
取消
