How to check if a variable is a function ?
Check if the variable is a Function
1 min readJun 22, 2018
This is an interesting and a pretty simple question. I did figure out few ways in which you can include this into your library. There are various ways to identify if the variable is a function. We will look for two different ways of achieving this out.
- Easiest and Best way
- function foo(v) {if (v instanceof Function) {/* do something */} };
- Next best way to do this
- function foo(v) {if (typeof v === “function”) {/* do something */} };
- The way I prefer
- function isFunction(functionToCheck) { var getType = {}; return functionToCheck && getType.toString.call(functionToCheck) === ‘[object Function]’; }
Now these are the various flows using which one can easily determine
Originally published at The Web Juice.