如何在JavaScript中檢查「未定義」? [重複] - How to check for 「undefined」 in JavaScript? [duplicate]

問題:

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?

解決方案:

參考一: https://stackoom.com/question/EDzo/如何在JavaScript中檢查-未定義-重複
參考二: https://oldbug.net/q/EDzo/How-to-check-for-undefined-in-JavaScript-duplicate
相關文章
相關標籤/搜索