Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_.isFunction(a) vs. typeof a === 'function'? javascript

I think it might be only performance case - http://jsperf.com/comparing-underscore-js-isfunction-with-typeof-function/2

And seems that typeof is faster.. so my question is - which is more appropriate to use?

like image 593
Kosmetika Avatar asked Jun 14 '13 12:06

Kosmetika


People also ask

What is the function of === in JavaScript?

Using Strict Equal (===) operator: In JavaScript, '===' Operator is used to check whether two entities are of equal values as well as of equal type provides a boolean result. In this example, we use the '===' operator. This operator, called the Strict Equal operator, checks if the operands are of the same type.

How do you check if it is a function in JavaScript?

Use the typeof operator to check if a function is defined, e.g. typeof myFunction === 'function' . The typeof operator returns a string that indicates the type of a value. If the function is not defined, the typeof operator returns "undefined" and doesn't throw an error.

How do you use typeof?

typeof is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined. There are other uses as well. The typeof operator is useful because it is an easy way to check the type of a variable in your code.

How do you check if it is a function?

Determining whether a relation is a function on a graph is relatively easy by using the vertical line test. If a vertical line crosses the relation on the graph only once in all locations, the relation is a function. However, if a vertical line crosses the relation more than once, the relation is not a function.


1 Answers

There is no reason not to use typeof.

Not only is it faster but the ECMAScript specification ensures that all functions have a type of "function" and that only functions can have a type of "function" :

enter image description here

This operator was specifically designed to get the type of a value, so why not use it ?

like image 156
Denys Séguret Avatar answered Sep 23 '22 13:09

Denys Séguret