Check if an object contains a function in JavaScript

Hello guys! Today we will share a guide on how to check if an object contains a function in JavaScript. This is the most basic skill when working with functions, so please look at the two solutions: using the constructor.name property and using the “typeof” operator.

Check if an object contains a function in JavaScript

First, we have to announce that there is a slight difference in the results returned by the following methods, and hence you should read carefully to see the differences before applying it to your code.

Method 1: Using constructor.name

Syntax:

objectName.functionName.constructor.name

Parameter:

  • objectName: the name of the object that contains a function
  • functionName: the name of the function you want to check

The constructor.name is a property of any object or function; if the object contains a function, this property of that function name will return “Function”, for example:

let myObject = {
    learnShareIt() {
        return "LearnShareIT"
    },
};

// Check if an object contains a function learnShareIt:
console.log(myObject.learnShareIt.constructor.name == "Function");
console.log(myObject.learnShareIt.constructor.name == "function");

Output:

true
false

The example above indicates that we have declared a function named learnShareIt inside the object. However, we receive two different results as we have checked it with the string “Function” and “function” respectively. You should bear in mind that if the object contains a function, this property will return the string “Function” but not “function”. Therefore, you can compare it with this string (“function”), and if it does contain, we will get the “true” boolean value as in the example above.

Method 2: Using typeof operator

Syntax:

typeof objectName.functionName

Parameter:

  • objectName: The name of the object that contains the function you want to check
  • functionName: The name of the function you want to check

With this operator, you can easily check the type of something in JavaScript. If it is a function, the type returned will be “function” (first letter is a lower “f” character). For instance:

let myObject = {
    learnShareIt() {
        return "LearnShareIT"
    },
};

// Check if an object contains a function learnShareIt:
console.log(typeof myObject.learnShareIt == "Function");
console.log(typeof myObject.learnShareIt == "function");

Output

false
true

Note that, using this operator, you have to put the operator “typeof” before the object name. As you can see, we have compared the result returned by this operator with the string “Function” and “function” in the same order as the first solution. However, we receive the false first because, as we have explained, if the type is a function, then this operator returns “function” instead of “Function”. Therefore, we must check the result with the string “function” to see whether an object contains a function or not.

Summary

We have learned How to check if an object contains a function in JavaScript. We recommend you use the second method of using the “typeof” operator and compare the returned value with “function” because it is what most programmers use. You can read more tutorials about Objects in JavaScript here. Have a nice day!

Leave a Reply

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