How To Check If A Function Is Defined In JavaScript

How to Check if a Function is Defined in JavaScript

Hello guys! Today we will share a guide on how to check if a function is defined in JavaScript. This is the most basic skill when you work with functions so please take a look at the below explanations and examples.

How to check if a function is defined in JavaScript

We are going to provide you with two different approaches. Each way is relatively easy so you can use any which you like.

Using typeof operator

Syntax:

typeof function

Parameter:

  • function: The function name which you want to check.

The typeof operator can be used to get the type of something in JavaScript and then if that is a defined function then the operator will return ‘function’:

For example:

// Define a function
function foo(){
	return LearnShareIT
}
 
// Check if a function is defined
if (typeof foo === 'function') {
  console.log('Function defined');
} else {
  console.log('Function undefined');
}

Output:

Function defined

The example above indicates that we have declared the function named foo() and the if statement is used with the typeof operator to check if it returns the string ‘function’ or not. Because it is a defined function we will get the result as in the output. Otherwise the result will be ‘Function undefined’.

Using constructor.name property

Syntax:

function.constructor.name

Parameter:

  • function: The function name which you want to check.

The constructor.name is one property of many type in JavaScript; you can read this property on the function name you want to check, if it is defined then ‘Function’ will be returned:

function foo(){
	return LearnShareIT
}
 
// Check if a function is defined
if (foo.constructor.name === 'function') {
  console.log('Function defined');
} else {
  console.log('Function undefined');
}

Output:

Function undefined

Why does the code below return an unexpected result like that? As you may suppose, it should be Function defined right? It is as we have said, this approach will return ‘Function’ when the function has been declared, but not ‘function’ like the first solution. So you must pay attention to the letter F as our language is case-sensitive. The correct way of checking it must be: 

function foo(){
	return LearnShareIT
}
 
// Check if a function is defined
if (foo.constructor.name === 'Function') {
  console.log('Function defined');
} else {
  console.log('Function undefined');
}

Output:

Function defined

As you can see, using this approach also helps you get the goal, and the result will be the same as the first one also. But you must remember to change the word ‘function’ to ‘Function’ before checking if a function is defined as these methods return different strings.

Summary

We have learned how to check if a Function is Defined in JavaScript in JavaScript. By using the typeof operator or the constructor.name property, you can easily do the task. We have lots of tutorials about JavaScript which you can find more in this link.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *