Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Can anyone tell me what’s the difference between callback instanceof Function” or typeof callback == "function"? 

1 Answer

0 votes
by (19.7k points)

For custom types, use instanceof:

var ClassFirst = function () {};

var ClassSecond = function () {};

var instance = new ClassFirst();

typeof instance; // object

typeof instance == 'ClassFirst'; // false

instance instanceof Object; // true

instance instanceof ClassFirst; // true

instance instanceof ClassSecond; // false 

For built-in types, use type of: 

'example string' instanceof String; // false

typeof 'example string' == 'string'; // true

'example string' instanceof Object; // false

typeof 'example string' == 'object'; // false

true instanceof Boolean; // false

typeof true == 'boolean'; // true

99.99 instanceof Number; // false

typeof 99.99 == 'number'; // true

function() {} instanceof Function; // true

typeof function() {} == 'function'; // true

For complex built-in types, use instanceof: 

/regularexpression/ instanceof RegExp; // true

typeof /regularexpression/; // object

[] instanceof Array; // true

typeof []; //object

{} instanceof Object; // true

typeof {}; // object

typeof null; // object

 If you want to learn more about Javathen go through this Java tutorial by Intellipaat for more insights.

Browse Categories

...