在Javascript中检查未定义

Kri*_*nan 9 javascript undefined

我按照以下逻辑检查变量是否未定义:

 if (variable==undefined){
////implementation
}
Run Code Online (Sandbox Code Playgroud)

但发现在某些情况下它没有按预期运行.所以,试过这种方法,

if(typeof(variable) == "undefined"){
/////implementation
}
Run Code Online (Sandbox Code Playgroud)

哪一个最可靠?

Asa*_*aph 6

您的第二种方式是最可靠的,但您不需要typeof操作符的括号.看到这个问题.