JavaScript

How to Check if Function Exists in JavaScript

Sometimes, developers may need to determine whether a particular function exists in the script. For example, if the programmer has multiple scripts on a page that use common functions, they may want to ensure that the function with the same name and functionality has already been defined. This helps to avoid naming conflicts and ensures that only one version of the function is available for use. Similarly, an error will occur when programmers access a function that is not defined in the scope. Therefore, checking if the function exists before calling can avoid the “uncaught reference error”.

This article will describe the methods to verify whether the specific function exists in the JavaScript file.

How to Check if Function Exists in JavaScript?

To verify whether the specified function is already present in JavaScript, use the following approaches:

Method 1: Check if the Function Exists in JavaScript Using the “typeof” Operator

To verify whether the function already exists in script or not, use the “typeof” operator. It returns the data type of a value or expression and outputs a string indicating the operand type, such as number, string, function, and so on.

Syntax
Follow the given syntax of the “typeof” operator to determine whether the function exists or not:

if (typeof functionName === "function") {
 //....
}

Or also you can use the below-mentioned syntax:

if (typeof functionName !== undefined) {
 //....
}

Example
Define a function “testing” in a JavaScript file:

function testing() {
 console.log("Welcome to LinuxHint");
}

Now, verify whether the “testing” function is already defined or not, using the “typeof” operator. The “typeof” operator checks the type of the “testing” function. If the returned value is equivalent to the “function, it will print the “The testing() function exists in the JS file” message. Else, “The testing() function does not exist in the JS file” message will be displayed:

if (typeof testing === "function") {
 console.log("The testing() function exists in the JS file");
}
else{
 console.log("The testing() function does not exist in the JS file");
}

Output

Method 2: Check if the Function Exists in JavaScript Using the “window” Object

You can also use the “window” object to check if a function exists or not. The window object serves as the browser window’s universal representation in the browser environment. It has various properties and methods for interacting with the window and its contents.

Syntax
The following syntax is utilized for determining the function exists using the “window” object:

if (window.functionName) {
 // ...
}

Example
Here, we will verify whether the function named “testing” exists in the JavaScript file using the “window” object:

if (window.testing) {
 console.log("The testing() function exists in the JS file");
}
else{
 console.log("The testing() function does not exist in the JS file");
}

The output indicates that the function “testing” exists in the JavaScript file:

That’s all about determining if the function exists in JavaScript.

Conclusion

To determine/check whether the function exists in JavaScript or not, use the “typeof” operator or the “window” object. The “typeof” operator is a commonly used and reliable method while you are not in the browser environment. Because, in that case, the “window” object may not be available. This article described the methods to verify whether the specific function exists in the JavaScript file or not.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.