• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Check if Variable is a Function

Using JavaScript to Check if Variable is a Function

June 13, 2022 Leave a Comment

One of the easiest ways we can use JavaScript to check if a variable is a function is by using the JavaScript typeof Operator in an if conditional statement.

if ( typeof someVariable == "function" ){
  //The variable IS a function
}

In the code above, someVariable is the variable that we want to check to see if it is a function. typeof someVariable will return “function” if the variable is a function, and therefore the conditional statement will be true.

We can put this code in a function to make checking if a variable is a function very simple.

Here is the function:

function isFunction(variable){
  if ( typeof variable == "function" ){
    return true;
  } else {
    return false;  
  }
};

Our function simply returns true if the variable you enter is a function, and false if not.

Now let’s show some examples of this function is use:

function isFunction(variable){
  if ( typeof variable == "function" ){
    return true;
  } else {
    return false;  
  }
};

var variable1 = {name:'tom', age:25};
var variable2 = [1,2,3,4];
var variable3 = "1234";
var variable4 = ["Red","Yellow","Green","Blue"];
var variable5 = function isFunction(variable){
  if ( typeof variable == "function" ){
    return true;
  } else {
    return false;  
  }
};
var variable6 = {};

console.log(isFunction(variable1));
console.log(isFunction(variable2));
console.log(isFunction(variable3));
console.log(isFunction(variable4));
console.log(isFunction(variable5));
console.log(isFunction(variable6));

#Output:
false
false
false
false
true
false

Hopefully this article has been useful for you to learn how to use JavaScript to check if a variable is a function.

Other Articles You'll Also Like:

  • 1.  How to Count Vowels in a String Using JavaScript
  • 2.  Using JavaScript to Add Seconds to a Date
  • 3.  Using JavaScript to Get a Random Number Between Range of Numbers
  • 4.  How to Uncheck All Checkboxes in JavaScript
  • 5.  JavaScript Random Boolean – How to Generate Random Boolean Values
  • 6.  How to Use JavaScript to Change Text in Span
  • 7.  Using Javascript to Check if Value is Not Undefined
  • 8.  JavaScript Array includes Method
  • 9.  How to Change an Image on Hover Using JavaScript
  • 10.  Using JavaScript to Remove Trailing Zeros From a Number or String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy