检查 Javascript 中是否未定义
我按照以下逻辑来检查变量是否未定义:
if (variable==undefined){
////implementation
}
但发现在某些情况下它没有按预期运行。那么,尝试了这种方法,
if(typeof(variable) == "undefined"){
/////implementation
}
那么哪种方法最可靠呢?
I was following the below logic to check if a variable is undefined or not:
if (variable==undefined){
////implementation
}
But found that for some cases it did not function as expected. So, tried this approach,
if(typeof(variable) == "undefined"){
/////implementation
}
So which one is most reliable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第二种方法是最可靠的,但您不需要
typeof
运算符的括号。请参阅此问题。Your second way is the most reliable but you don't need the parenthesis for the
typeof
operator. See this question.这种方式比第二种选择更有用
this way is more use full than second option