如何在JavaScript中检查null,undefined或空白变量?

获得以下结果后,您可以查找变量为null还是未定义。如果结果为“ false”,则表示变量为null且未定义。

在这里,变量结果为“ True”-

<html>
   <body>
      <script>
         var age = 10;
            if(age){
               document.write("True");
            } else{
               document.write("False");
            }
      </script>
   </body>
</html>

使用“ typeof”检查变量是否存在-

<html>
   <body>
      <script>
         var age = 10;
         if( typeof age !== 'undefined' ) {
            document.write("True");
         } else{
            document.write("False");
         }
      </script>
   </body>
</html>