Skip to content

How to Check if a Variable is of Function Type

There are several methods in JavaScript that can check whether a variable is of function type. Let’s discuss each of them and find the fastest solution to the question.

The typeof Operator

You can use the typeof operator to check the variable. It returns a string which indicates the type of the unevaluated operand.

Javascript typeof method

Output appears here after Run.

The instanceof Operator

Another method can be the instanceof operator which checks the type of an object at run time. It returns a corresponding boolean value, for example, either true or false to indicate if the object is of a particular type or not:

Javascript instanceof method

Output appears here after Run.

INFO

The typeof operator appears to be the fastest in Chrome, but in Firefox the instanceof operator is the winner.

Dual-run preview — compare with live Symfony routes.