Answer by Maciej Wakuła for Check if a variable is of function type
This is a bit tricky question as you need to define "what is a function?" first.Let's look at node (V8 implementation of JavaScript engine). It is written in C++ but also has parts in JavaScript.Some...
View ArticleAnswer by Jannis Ioannou for Check if a variable is of function type
There are functions whose arguments can be passed either as a value or though a callback. To implement them a nice helper function that utilizes type detection is the following:const toCallback =...
View ArticleAnswer by Teocci for Check if a variable is of function type
This is an old question but there are some considerations in 2022:First, browser compatibility: instanceof is supported by all modern browsers as well as Deno and NodeJS.Also, it's syntactically...
View ArticleAnswer by De Bonheur for Check if a variable is of function type
if you are looking for a simple solution: function isFunction(value) { return value instanceof Function}
View ArticleAnswer by Yassine CHABLI for Check if a variable is of function type
Becareful about this :typeof Object === "function" // true.typeof Array === "function" // true
View ArticleAnswer by Alireza for Check if a variable is of function type
Something with more browser support and also include async functions could be:const isFunction = value => value ? (Object.prototype.toString.call(value) === "[object Function]" || "function" ===...
View ArticleAnswer by Damaged Organic for Check if a variable is of function type
For those who's interested in functional style, or looks for more expressive approach to utilize in meta programming (such as type checking), it could be interesting to see Ramda library to accomplish...
View ArticleAnswer by saeta for Check if a variable is of function type
If you use Lodash you can do it with _.isFunction._.isFunction(function(){});// => true_.isFunction(/abc/);// => false_.isFunction(true);// => false_.isFunction(null);// => falseThis method...
View ArticleAnswer by Danny Mor for Check if a variable is of function type
I think you can just define a flag on the Function prototype and check if the instance you want to test inherited thatdefine a flag:Function.prototype.isFunction = true; and then check if it existvar...
View ArticleAnswer by wsc for Check if a variable is of function type
const foo = function() {};if (typeof foo === 'function') { console.log('is function')}
View ArticleAnswer by Marcus Junius Brutus for Check if a variable is of function type
The below seems to work for me as well (tested from node.js):var isFunction = function(o) { return Function.prototype.isPrototypeOf(o);};console.log(isFunction(function(){})); //...
View ArticleAnswer by moklick for Check if a variable is of function type
Since node v0.11 you can use the standard util function :var util = require('util');util.isFunction('foo');
View ArticleAnswer by GuillaumeL for Check if a variable is of function type
An other simply way:var fn = function () {}if (fn.constructor === Function) { // true} else { // false}
View ArticleAnswer by Cesar Loachamin for Check if a variable is of function type
jQuery (deprecated since version 3.3) Reference$.isFunction(functionName);AngularJSReferenceangular.isFunction(value);LodashReference_.isFunction(value);UnderscoreReference_.isFunction(object); Node.js...
View ArticleAnswer by Rajesh Paul for Check if a variable is of function type
you should use typeOf operator in js.var a=function(){ alert("fun a");}alert(typeof a);// alerts "function"
View ArticleAnswer by dalimian for Check if a variable is of function type
There are several ways so I will summarize them allBest way is: function foo(v) {if (v instanceof Function) {/* do something */} };Most performant (no string comparison) and elegant solution - the...
View ArticleAnswer by Azmisov for Check if a variable is of function type
I found that when testing native browser functions in IE8, using toString, instanceof, and typeof did not work. Here is a method that works fine in IE8 (as far as I know):function isFn(f){ return !!(f...
View ArticleAnswer by user1176126 for Check if a variable is of function type
Try the instanceof operator: it seems that all functions inherit from the Function class:// Test datavar f1 = function () { alert("test"); }var o1 = { Name: "Object_1" };F_est = function () { };var o2...
View ArticleAnswer by dandean for Check if a variable is of function type
@grandecomplex: There's a fair amount of verbosity to your solution. It would be much clearer if written like this:function isFunction(x) { return Object.prototype.toString.call(x) == '[object...
View ArticleAnswer by Alex Grande for Check if a variable is of function type
Sure underscore's way is more efficient, but the best way to check, when efficiency isn't an issue, is written on underscore's page linked by @Paul Rosania.Inspired by underscore, the final isFunction...
View ArticleAnswer by Paul Rosania for Check if a variable is of function type
Underscore.js uses a more elaborate but highly performant test:_.isFunction = function(obj) { return !!(obj && obj.constructor && obj.call && obj.apply);};See:...
View ArticleAnswer by selbie for Check if a variable is of function type
if (typeof v === 'function') { // do something}
View ArticleCheck if a variable is of function type
Suppose I have any variable, which is defined as follows:var a = function() {/* Statements */};I want a function which checks if the type of the variable is function-like. i.e. :function foo(v) {if (v...
View Article
More Pages to Explore .....