如何检查 JavaScript 函数是否已定义?

javascriptweb developmentfront end technology

要检查 JavaScript 函数是否已定义,请使用"undefined"进行检查。

示例

您可以尝试运行以下示例来检查 JavaScript 中函数是否已定义 −

<!DOCTYPE html>
<html>
   <body>
      <script>
         function display() {
            alert("Demo Text!");
         }

         if ( typeof(display) === 'undefined') {
            document.write('undefined');
         } else {
            document.write("Function is defined");
         }
      </script>
   </body>
</html>

相关文章