js判断undefined类型

看到同事写的前端代码判断undefind:

 

 

 发现判断不出来,最后查了下资料要用typeof
方法:
if (typeof(reValue) == "undefined") {
   alert("undefined");
}  
typeof 返回的是字符串,有六种可能:"number"、"string"、"boolean"、"object"、"function"、"undefined"

https://www.jb51.net/article/48512.htm

实践出真知。下面的代码又会怎样呢

       var o = {};
    
    //if(typeof(abc) == "undefined")  // ok
    //if( abc == undefined ) // error
    if( o.abc == undefined )  // ok
    {
        alert('abc undefined'); return;
    }

 

posted @ 2021-12-17 14:27  晨光静默  阅读(216)  评论(0编辑  收藏  举报