Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A more refined Javascript typeof? [duplicate]

Is there a way for typeof to return what an object is?

typeof {};

returns "object"

typeof [];

also returns "object". Is there a way in js to return "array"?

On top of that, is there a way to tell if an object is a DOM object, a javascript object or whatever object?

like image 805
Gundam Meister Avatar asked Feb 23 '16 19:02

Gundam Meister


1 Answers

You can't extend the typeof operator but for better type inspection you can (ab)use Object.prototype.toString

Object.prototype.toString.call([]) === '[object Array]'
Object.prototype.toString.call(document) === '[object HTMLDocument]`
like image 108
Mike Cluck Avatar answered Sep 19 '22 23:09

Mike Cluck