如何在 JavaScript 中檢查變數是否存在?
要在 JavaScript 中檢查變數是否存在,你需要像以下程式碼中那樣將其與 null 進行比較。我們在此檢查變數 myVar -
<html> <body> <script> var myVar = 20; if(myVar !== undefined && myVar !== null) { document.write("Variable exists"); } </script> </body> </html>
廣告
要在 JavaScript 中檢查變數是否存在,你需要像以下程式碼中那樣將其與 null 進行比較。我們在此檢查變數 myVar -
<html> <body> <script> var myVar = 20; if(myVar !== undefined && myVar !== null) { document.write("Variable exists"); } </script> </body> </html>