如何在JavaScript中检查“未定义”? [重复]

Deadly 提交于 2020-03-27 17:41:16

3 月,跳不动了?>>>

问题:

What is the most appropriate way to test if a variable is undefined in JavaScript? 测试JavaScript中是否未定义变量的最合适方法是什么? I've seen several possible ways: 我已经看到了几种可能的方法:

if (window.myVariable)

Or 要么

if (typeof(myVariable) != "undefined")

Or 要么

if (myVariable) //This throws an error if undefined. Should this be in Try/Catch?

解决方案:

参考一: https://stackoom.com/question/EDzo/如何在JavaScript中检查-未定义-重复
参考二: https://oldbug.net/q/EDzo/How-to-check-for-undefined-in-JavaScript-duplicate
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!