Advertisement
  1. Code
  2. Language Fundamentals

How to Check If a Function Exists in Java Script

Scroll to top
Read Time: 3 min

You've probably seen the undefined error in the console when you try to call a function you have not defined in your JavaScript code. JavaScript throws this error and stops running the code.

In this article, I will teach you how to check if a function exists. This way, you can avoid any possible errors. This is a useful technique to see if a specific library or API is available in the client you're running your software on.

JavaScript has a few different ways to see if a function exists. I'll show you several.

Use an if Conditional Statement

One way to check if a function is defined is to test it with an if statement. The trick is to test the function as a method of the window object. 

So, if you want to test for aFunctionName, just use:

1
if (window.aFunctionName) {
2
    // ...

3
}

The code in the brackets will execute if the function is defined. If instead you just test the function without using the window object, for example as if(aFunctionName),  JavaScript will throw a ReferenceErorr if the function doesn't exist.

Let's consider the following example, which checks for the existence of two functions: one that exists and another that does not exist.

1
// Testing a function that exists

2
function exists() {
3
    // ...

4
}
5
6
if (window.exists) {
7
    console.log('the exists() function exists');
8
}
9
else{
10
    console.log('the exists() function does not exist');
11
}
12
13
if(window.doesntExist) {
14
    console.log('the doesntExist() function exists');
15
}
16
else{
17
    console.log('the doesntExist() function does not exist');
18
}

The above code snippet will output this:

1
the exists() function exists
2
the doesntExist() does not exist

This worked great for our example, but a problem with that approach is that we aren't checking if the named object is actually a function. In fact, any variable with the same name would fool our test into thinking the function is defined.

The typeof Operator 

Alternatively, we can use the typeof operator. This operator will check whether the name of the declared function exists and whether it is a function and not some other type of object or primitive.

1
if (typeof nameOfFunction === 'function') {
2
    nameOfFunction();
3
}

In the example above, we test if nameOfFunction exists, and if it does we run it.

Use a try…catch Block

The try…catch block handles errors that are likely to occur within that block. We will use this method to handle the undefined error we expect JavaScript to throw when we call a function that we have not defined.

How the try...catch Statement Works

We run the function inside the try block. If it doesn't exist, an exception will be thrown and will be handled by the catch block.

Here's an example:

1
try {
2
    testFunction();
3
} 
4
catch(err) {
5
    console.log(err);
6
}

If testFunction is not defined, this will output the following message to the console.

1
ReferenceError: testFunction is not defined

This is what we would see without a try...catch block as well, but in this case our code will continue to run below the catch block.

Conclusion 

This article has covered three main methods to check whether a function exists in JavaScript before we call it. These are the use of an if conditional statement, the use of a typeof operator, and finally, the try...catch statement. I've also used examples to explain how JavaScript can implement these methods to check whether a function exists or not. I hope this concept is clearer to you now!. 

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.