如何检查 JavaScript 中是否存在变量?

javascriptweb developmentfront end technology

要检查 JavaScript 中是否存在变量,您需要检查它是否为 null,如以下代码所示。在这里,我们检查变量 myVar 是否存在 −

<html>
   <body>
      <script>
         var myVar = 20;
         if(myVar !== undefined && myVar !== null) {
            document.write("Variable exists");
         }
      </script>
   </body>
</html>

相关文章


有用资源