Check if a variable is of the function type

Asked

Viewed 57 times

0

Let’s say I have some variable, defined as:

var xpto = function() {/* Faz alguma coisa */};

I need to compare if the type of the variable is a function, ie:

function isFunction(f) {if (f is (function type?)) {/* ... */}};
isFunction(xpto);

So how can I check whether the variable xpto is the type function?

2 answers

5


Knowing that:

typeof xpto // "function"

Maybe do:

function isFunction(f) {
  return (typeof f === "function")
}

Be an option for what you want. I hope I’ve helped.

4

Browser other questions tagged

You are not signed in. Login or sign up in order to post.