- How to check if a function exists in JavaScript
- Handling non-existent function with try..catch block
- Learn JavaScript for Beginners 🔥
- About
- Search
- Tags
- How to Check if Function Exists in JavaScript
- The try. catch Method
- The typeof Operator
- How to Check If a Function Exists in Java Script
- Use an if Conditional Statement
- How to check if function exists in javascript?
- Method 1: typeof operator
- Method 2: Object.prototype.hasOwnProperty() method
- Method 3: try. catch statement
- Method 4: The Window Object Method
- How to Check if Function Exists in JavaScript
- The try. catch Method
- The typeof Operator
How to check if a function exists in JavaScript
An error caused by calling a non-existent function will cause JavaScript to throw an undefined error and stop running your code. To prevent the error, you can first check if a function exists in your current JavaScript environment by using a combination of an if statement and the typeof operator on the function you want to call.
Only when the typeof returns «function» will you call that function to execute it. Let’s look at the following example:
When the type of printMessage is anything other than "function" , you don’t call the function.
Handling non-existent function with try..catch block
Alternatively, you can also use a try..catch block to handle the error when a non-existent function is called:
Without the try..catch block, the console logs above won’t be executed by JavaScript. You can use the techniques above to check if a JavaScript function exists before calling it.
Learn JavaScript for Beginners 🔥
Get the JS Basics Handbook, understand how JavaScript works and be a confident software developer.
A practical and fun way to learn JavaScript and build an application using Node.js.
About
Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.
Search
Type the keyword below and hit enter
Tags
Click to see all tutorials tagged with:
How to Check if Function Exists in JavaScript
There are times when you get an error while calling a function that has not been defined. Here, we suggest two methods to check if the function exists.
To check if a particular function name has been defined, you can use the typeof operator:
if (typeof myFunctionName === 'function') < myFunctionName(); >
In the given case, the typeof operator will return undefined because myFunctionName() has not been defined. So, the function call inside the IF statement won’t be executed.
If the function exist, the typeof operator will return the string “function”:
Javascript typeof operator
The try. catch Method
There is also another method of try. catch statement that catches function ReferenceError errors. This approach wraps the function call inside the try block of the statement like this:
Javascript try. catch method
If the function does not exist, the error occurs.
The typeof Operator
The typeof operator gets the data type of the unevaluated operand which can be a literal or a data structure like an object, a function, or a variable. The typeof returns a string with the name of the variable type as a first parameter (object, boolean, undefined, etc.). Before ECMAScript 2015, typeof was always returned a string for any operand it was supplied with. It could never generate an error. But after the addition of let and Statements/const using typeof on let and const variables in a block before they’re declared will generate a ReferenceError.
How to Check If a Function Exists in Java Script
Neema Muganga Last updated Feb 24, 2022
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:
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.
// Testing a function that exists
How to check if function exists in javascript?
In JavaScript, it’s often necessary to determine whether a function exists or not, particularly when working with code from external sources. This can become especially important when you need to call a function that might not exist yet, for example, when working with dynamically loaded scripts or 3rd party libraries. Fortunately, there are a few ways to check if a function exists in JavaScript.
Method 1: typeof operator
To check if a function exists in JavaScript, you can use the typeof operator. Here’s how you can do it:
// Define a function function myFunction() console.log('Hello World!'); > // Check if the function exists if (typeof myFunction === 'function') // Call the function myFunction(); >
In this example, we define a function called myFunction . We then use the typeof operator to check if the myFunction variable is a function. If it is, we call the function.
// Check if a function exists if (typeof nonExistentFunction === 'function') // This code won't be executed nonExistentFunction(); > else console.log('The function does not exist.'); >
In this example, we check if a function called nonExistentFunction exists. Since this function does not exist, the else block will be executed and a message will be logged to the console.
That’s it! Using the typeof operator is a simple and effective way to check if a function exists in JavaScript.
Method 2: Object.prototype.hasOwnProperty() method
To check if a function exists in JavaScript, you can use the hasOwnProperty method of the Object.prototype object. Here is an example code snippet that demonstrates how to do this:
if (Object.prototype.hasOwnProperty.call(window, 'myFunction')) // myFunction exists myFunction(); > else // myFunction does not exist console.log('myFunction does not exist'); >
In this example, we are checking if the myFunction function exists in the global window object. We use the hasOwnProperty method of the Object.prototype object to check if the myFunction property exists on the window object.
If myFunction exists, we call it. If it does not exist, we log a message to the console.
Here is another example that demonstrates how to check if a function exists in an object:
const myObject = myFunction: function() console.log('Hello, world!'); > >; if (Object.prototype.hasOwnProperty.call(myObject, 'myFunction')) // myFunction exists myObject.myFunction(); > else // myFunction does not exist console.log('myFunction does not exist'); >
In this example, we are checking if the myFunction function exists in the myObject object. We use the hasOwnProperty method of the Object.prototype object to check if the myFunction property exists on the myObject object.
If myFunction exists, we call it using the dot notation ( myObject.myFunction() ). If it does not exist, we log a message to the console.
Overall, the hasOwnProperty method of the Object.prototype object is a useful tool for checking if a function exists in JavaScript.
Method 3: try. catch statement
To check if a function exists in JavaScript, you can use the «try. catch statement» to handle any errors that may occur when attempting to call the function. Here’s an example code:
function checkFunctionExists(functionName) try return typeof eval(functionName) === 'function'; > catch (e) return false; > > console.log(checkFunctionExists('nonExistentFunction')); // false console.log(checkFunctionExists('console.log')); // true console.log(checkFunctionExists('Array.isArray')); // true
In this example, the checkFunctionExists function takes a string parameter functionName , which is the name of the function you want to check. The function then attempts to evaluate the function name using the eval function, and checks if the result is a function using the typeof operator. If any errors occur during this process, the function returns false .
You can use this function to check if any function exists in your code, such as built-in functions like console.log or Array.isArray .
Method 4: The Window Object Method
To check if a function exists in JavaScript using the Window Object Method, you can use the following code:
if (typeof window.functionName === "function") // The function exists > else // The function does not exist >
This code checks if the functionName function exists in the global window object. If the function exists, the code inside the first block will be executed. Otherwise, the code inside the second block will be executed.
Here’s another example that checks if a function exists within a specific object:
var myObject = myFunction: function() // Function code here > >; if (typeof myObject.myFunction === "function") // The function exists > else // The function does not exist >
This code checks if the myFunction function exists within the myObject object. If the function exists, the code inside the first block will be executed. Otherwise, the code inside the second block will be executed.
You can also use the in operator to check if a function exists within an object:
if ("myFunction" in myObject) // The function exists > else // The function does not exist >
This code checks if the myFunction function exists within the myObject object using the in operator. If the function exists, the code inside the first block will be executed. Otherwise, the code inside the second block will be executed.
In summary, to check if a function exists in JavaScript using the Window Object Method, you can use the typeof operator or the in operator to check if the function exists within the global window object or a specific object.
How to Check if Function Exists in JavaScript
There are times when you get an error while calling a function that has not been defined. Here, we suggest two methods to check if the function exists.
To check if a particular function name has been defined, you can use the typeof operator:
if (typeof myFunctionName === 'function') < myFunctionName(); >
In the given case, the typeof operator will return undefined because myFunctionName() has not been defined. So, the function call inside the IF statement won’t be executed.
If the function exist, the typeof operator will return the string “function”:
Javascript typeof operator
The try. catch Method
There is also another method of try. catch statement that catches function ReferenceError errors. This approach wraps the function call inside the try block of the statement like this:
Javascript try. catch method
If the function does not exist, the error occurs.
The typeof Operator
The typeof operator gets the data type of the unevaluated operand which can be a literal or a data structure like an object, a function, or a variable. The typeof returns a string with the name of the variable type as a first parameter (object, boolean, undefined, etc.). Before ECMAScript 2015, typeof was always returned a string for any operand it was supplied with. It could never generate an error. But after the addition of let and Statements/const using typeof on let and const variables in a block before they’re declared will generate a ReferenceError.