How to check if a function exists in JavaScript? – Solutions

Today, we are going to deal with Javascript check if a function exists on object so that you can avoid any possible errors.

Checking if a function exists in JavaScript is dominant for developers and programmers.

This article shows different methods to do it, with practical examples that help you in your programming journey.

How to JavaScript check if a function exists?

If a programmer has multiple scripts on a page that use the same functions, it’s dominant to check if those functions have been defined.

It prevents conflicts and avoids errors when accessing undefined functions, like the “uncaught reference error.”

To check if a function exists in JavaScript, you can use an if statement along with the typeof operator to check if a function exists in your current JavaScript environment before calling it.

Solution 1: Use the typeof operator

To check if a function exists in JavaScript, you can use the typeof operator.

In JavaScript, the typeof operator helps us determine the data type of a value. Interestingly, it can also be used to check if a function exists.

By applying the typeof operator to a function and checking if it returns function, we can easily determine if the function is available.

For example:

function sample() {
  
}

if (typeof sample === "function") {
  console.log("Hi, Welcome to Itsourcecode!");
} else {
  console.log("This is just a sample.");
}

In this example, when the typeof operator is applied to a function name that has not been defined (like sample and functionName()), it will return “undefined”.

As a result, the function call inside the if statement will not be executed.

On the other hand, if the function does exist, the typeof operator will return the string “function”.

As you can see in the given example above, we use the syntax:

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

Aside from that, you can also use the following syntax:

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

The two given syntax has thesame output.

Output:

Hi, Welcome to Itsourcecode!

Solution 2: Use try try…catch block

If you are not sure about your code and if the function really exists in your code, you have to JavaScript check if a function exists.

You can use a try…catch block to handle the situation where you call a function that doesn’t exist.

The try…catch block is used to handle errors that might occur within a specific code block.

In this example below, we will use it to handle the “undefined” error that JavaScript throws when we try to call a function that has not been defined.

For example:

try {
    sampleFunction();
} 
catch(error) {
    console.log(error);
}

If the function sampleFunction is not defined, the console will output the corresponding message.

Output:

ReferenceError: sampleFunction is not defined

Solution 3: Combine the typeof operator with the comparison against undefined

Another way to check if a function exists in JavaScript on object is by combining the typeof operator with a comparison to undefined.

If the variable holding the function is defined and its type is “function,” then we can be certain that the function exists.

For example:

var exists = function() {
 
};

if (typeof exists !== "undefined" && typeof exists === "function") {
  console.log("Hi, Welcome to Itsourcecode!");
} else {
  console.log("The function does not exist.");
}

In this example, we create a variable called “exists” and assign a function to it.

Then, we check if the variable is both defined and of type “function” using the typeof operator.

If it meets these criteria, we output the message “Hi, Welcome to Itsourcecode!” Otherwise, if the variable doesn’t meet either condition, we output the message “The function does not exist.”

Output:

Hi, Welcome to Itsourcecode!

Conclusion

In conclusion, this article explores different ways to check if a function exists in JavaScript. It highlights the importance of performing this check to prevent errors and conflicts.

The three solutions presented are:

  1. Using the typeof operator: By applying typeof to a function and checking if it returns “function”, you can determine if the function exists.

  1. Using the try…catch block: This approach handles errors when calling a function that doesn’t exist by using a try…catch block.

  1. Combining typeof operator with a comparison to undefined: By checking if the function variable is both defined and has a type of “function”, you can verify the existence of the function.

These methods provide developers with options to ensure the presence of functions before using them, consequently enhancing code reliability.

We are hoping that this article provides you with enough information that helps you understand if you want to ensure that the function truly exists in your code, Javascript check if a function exists is the dominant thing to do.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment