To check if a function exists in JavaScript, you can use either the “typeof” operator or the “try-catch block”.
Method 1: Using the typeof operator
The typeof operator checks whether the name of the declared function exists and whether it is a function and not some other type of object or primitive.
Syntax
if (typeof name_of_function === 'function') {
name_of_function();
}
Example 1
function mainFunction() {
console.log('Hello, world!');
}
if (typeof mainFunction === 'function') {
mainFunction(); // Calls the function if it exists
} else {
console.log('The function does not exist.');
}
Output
In this example, typeof mainFunction will return the string ‘function’ if mainFunction is a function.
If it’s not a function or not defined, the typeof mainFunction will return a different string (e.g., ‘undefined’ or ‘object’).
By checking if typeof mainFunction === ‘function’, you can ensure that you only call the function if it exists.
Example 2
You can use a similar approach if you work with object methods or functions in a particular namespace.
const mainObject = {
mainMethod: function () {
console.log('Hello, world!');
},
};
if (typeof mainObject.mainMethod === 'function') {
mainObject.mainMethod(); // Calls the method if it exists
} else {
console.log('The method does not exist.');
}
Output
In this example, the function mainMethod is a property of the mainObject object.
By checking if typeof mainObject.mainMethod === ‘function’, you can ensure that you only call the method if it exists.
Method 2: Using the try-catch block
The try…catch block handles errors likely to occur within that block. For example, we will use this approach to handle the undefined error we expect JavaScript to throw when we call a function that we have not defined.
Syntax
try {
mainFunction();
}
catch(err) {
console.log(err);
}
Example
function mainFunction() {
console.log('Hello, world!');
}
try {
mnFunction();
}
catch (err) {
console.log(err);
}
Output
ReferenceError: mnFunction is not defined
You can see that the mnFunction() is not defined and got the error message output to the console.
Bonus method: Using an if-else statement
The if-else approach is beneficial if you use JavaScript in the browser. However, if you use this approach on Node.js, it won’t work because the window object does not exist in Node.js but only in Browser.
We will check the function as a method of the “window” object.
Syntax
if (window.mainFunction) {
// ...
}
Example
function mainFunction() {
console.log('Hello, world!');
}
if (window.mainFunction) {
console.log('The mainFunction() function is defined');
}
else {
console.log('The mainFunction() function is not defined');
}
Output
The mainFunction() function is defined
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.