Why javascript's typeof always return "object"?

The Student picture The Student · Sep 24, 2010 · Viewed 34.4k times · Source

What's it used for if it always returns object as type?

always for Elements or lists.

Answer

Spudley picture Spudley · Sep 24, 2010

JS's typeof doesn't always return 'object', but it does return object for things which people may not consider to be objects -- ie arrays, and also, oddly, for nulls.

For arrays this is correct, because as far as JS is concerned, arrays are objects; they're the same thing. Array is just another class, and you can instantiate objects of type Array, but they're still treated as objects.

This page has a list of types in JS, along with the response you'll get for each of them from typeof. It also has some JS code to override the typeof function with one that returns more useful information. If you're worried about it not being useful, you could implement something like that if you wish.