This tutorial shows how to check if a given variable is of function type. In JavaScript, we use typeof
keyword to find the type of a given variable.
Example 1: Printing the Type
const printHello = () => {
console.log(" Hello! ");
};
console.log("The type of printHello is: ",typeof printHello);
Output
The type of printHello is: function
Example 2: Check if the Given Variable is Function
function printHello () {
console.log(" Hello! ")
}
if (typeof printHello == 'function') {
console.log("The given variable is a function");
}
Output
The given variable is a function