Vino
Vino

Reputation: 304

Checking the equality of different kind of functions in Javascript

function a(){ return true; }
var b = function(){ return true; };
window.c = function(){ return true; };


console.log(typeof a);//returns funtion
console.log(typeof b);  //returns funtion
console.log(typeof window.c);   //returns funtion

typeof a === typeof b === typeof window.c  //returns false

On running the above code in the console , the final statement gives false. whereas, typeof all the 3 functions returns function. I know that there are some weird parts in javascript with typeof ..Can you guys explain this please..

Upvotes: 1

Views: 45

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

The problem has nothing to do with the types not being equal but the fact that:

a === b === c

is interpreted as:

(a === b) === c

So this means that the first test typeof a === typeof b is resolved as true and now you perform an equality check like true === typeof window.c.

You can solve the problem by rewriting the condition to:

(typeof a === typeof b) && (typeof b === typeof window.c)

Upvotes: 3

Related Questions