JavaScript Check if Function Exists
JavaScript gives us a method to check if a function exists or not. We can check it using the typeof operator and an if condition.
Using typeof Operator
To check if a function exists in JavaScript or not, we can use the typeof operator. This operator will check whether the name of the declared function exists and also whether it is a function or not. Here is a syntax:
Syntax:
if (typeof myFunction === "function") {
// myFunction is defined
myFunction();
} else {
// myFunction is not defined
console.log("myFunction is not defined");
}
To understand it clearly, let’s write a code to check, if the hello
function exists or not.
Example
<script> function hello() { document.write("This text will be displayed if a function exists") } if (typeof hello === "function") { hello(); } else { document.write("hello is not defined"); } </script>
Since the hello
function exists in the code, It will execute the function.
Example Explained
In an above example, we are checking if the hello
function is defined or not using the typeof operator. Since the hello
function is defined, it returns the “function” and the code inside the if block will be executed. If hello
was not defined, it will return undefined and the code inside the else block will be executed.
Let’s take a look at another example:
Example
<script> function hello() { document.write("This text will be displayed if a function exists") } if (typeof bye === "function") { bye(); } else { document.write("bye is not defined"); } </script>
In this example, we are checking if the bye
function is defined or not. Since the bye
function is not defined, it will not return the “function” and the code inside the else block will be executed.
Run this code on our Online Code Editor to see how it works.
Using if
Condition
Another way to check if a JavaScript function exists or not is by using an if conditional statement.
Here we have to test the function as a method of the window
object. Here is a syntax:
Syntax
if (window.myFunction) {
// myFunction is defined
myFunction();
}
else {
// myFunction is not defined
console.log("myFunction is not defined");
}
To understand it clearly, let’s write a code to check, if the world
function exists or not.
Example
<script> function world() { document.write("World function"); } if (window.world) { world(); } else { document.write("world function does not exist") } </script>
In an above example, we are checking if the world
function is defined or not. Since the world
function is defined, it returns the “function” and the code inside the if block will be executed. If the world
was not defined, it will return undefined and the code inside the else block will be executed.
Let’s take a look at the another example and here we check the function that doesn’t exist.
Example
<script> function world() { document.write("World function"); } if (window.earth) { world(); } else { document.write("earth function does not exist") } </script>
In an above example, we are checking if the earth
function is defined or not. Since the earth
function is not defined, the code inside the else block will be executed.
Share This Post!