Quantcast
Channel: Check if a variable is of function type - Stack Overflow
Browsing latest articles
Browse All 23 View Live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer by Yassine CHABLI for Check if a variable is of function type

Becareful about this :typeof Object === "function" // true.typeof Array === "function" // true

View Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer by wsc for Check if a variable is of function type

const foo = function() {};if (typeof foo === 'function') { console.log('is function')}

View Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer by selbie for Check if a variable is of function type

if (typeof v === 'function') { // do something}

View Article


Check 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
Browsing latest articles
Browse All 23 View Live




Latest Images