JavaScript Program to Check If a Variable is of Function Type

Updated on September 30, 2024
Check If a Variable is of Function Type header image

Introduction

In JavaScript, functions are first-class citizens which means they can be passed as arguments to other functions, returned as values from other functions, and assigned to variables. This flexibility necessitates the ability to distinguish whether a variable is a function to ensure proper handling in code logic.

In this article, you will learn how to verify if a variable holds a function using various techniques in JavaScript. Explore practical code examples that demonstrate how to perform these checks effectively, ensuring robust program execution.

Using typeof Operator

Basic Function Type Check

  1. Define a variable with a function value.

  2. Use the typeof operator to determine if the variable is a function.

    javascript
    function myFunction() {
        return "Hello, world!";
    }
    
    let checkFunction = typeof myFunction === 'function';
    console.log(checkFunction);
    

    This code checks if myFunction is of type 'function'. The typeof operator returns a string indicating the type of the operand. Here, it returns true because myFunction is indeed a function.

Using typeof with Non-Function Variable

  1. Assign a non-function value to a variable.

  2. Apply the typeof operator to verify the type.

    javascript
    let myVariable = 42;
    let isFunction = typeof myVariable === 'function';
    console.log(isFunction);
    

    Since myVariable contains a number, the typeof operation returns false, indicating that it's not a function.

Advanced Usage with Higher-Order Functions

Passing a Function as an Argument

  1. Create a higher-order function that checks if its argument is a function.

  2. Pass various types of variables to see the results.

    javascript
    function checkIfFunction(input) {
        return typeof input === 'function';
    }
    
    console.log(checkIfFunction(function() {})); // true
    console.log(checkIfFunction(12345));         // false
    console.log(checkIfFunction('Hello'));      // false
    

    This snippet effectively demonstrates checking the type of various inputs where only the function type passes the test.

Using Function.prototype.toString

Confirm Function Source Code

  1. Identify the necessity to differentiate between native and user-defined functions.

  2. Utilize Function.prototype.toString and search to achieve this.

    javascript
    function userFunction() {
        return true;
    }
    
    let isNativeFunction = Function.prototype.toString.call(userFunction)
                           .search(/\[native code\]/) !== -1;
    console.log(isNativeFunction); // false
    

    This code investigates whether userFunction is a native JavaScript function. It's not, as indicated by the false result. Native functions contain [native code] within their string representation.

Conclusion

Understanding how to determine if a variable is a function in JavaScript is crucial for handling various operations, especially in dynamic and large-scale applications. By utilizing the typeof operator and the Function.prototype.toString method, you can robustly ascertain a variable's function status. Implement these methods to maintain code integrity and avoid runtime errors due to misinterpretation of data types. Through these examples, ensure your JavaScript code remains reliable, clear, and functional.