This question already has an answer here: 這個問題已經在這裏有了答案: javascript
What is the most appropriate way to test if a variable is undefined in JavaScript? 測試JavaScript中是否未定義變量的最合適方法是什麼? I've seen several possible ways: 我已經看到了幾種可能的方法: java
if (window.myVariable)
Or 要麼 app
if (typeof(myVariable) != "undefined")
Or 要麼 測試
if (myVariable) //This throws an error if undefined. Should this be in Try/Catch?