Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why javascript's typeof always return "object"?

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

always for Elements or lists.

like image 244
The Student Avatar asked Sep 24 '10 14:09

The Student


People also ask

Why does typeof null return object?

The reasoning behind this is that null , in contrast with undefined , was (and still is) often used where objects appear. In other words, null is often used to signify an empty reference to an object. When Brendan Eich created JavaScript, he followed the same paradigm, and it made sense (arguably) to return "object" .

Why is object object being returned?

[object Object] is a string version of an object instance. This value is returned by a JavaScript program if you try to print out an object without first formatting the object as a string. It's no wonder developers get confused about this object: there are no error messages or warnings that tell us what is going on.

Why typeof array is object in JS?

This because in javascript all derived data type is always a type object. Included functions and array. In case you need to check if it's an array you can use isArray method of Array. and the result will be the same as the previous one.

Why in JavaScript everything is object?

Nearly everything in JavaScript is an object other than six things that are not objects which are — null , undefined , strings, numbers, boolean, and symbols. These are called primitive values or primitive types.


4 Answers

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.

like image 166
Spudley Avatar answered Oct 23 '22 18:10

Spudley


It doesn't always return "object":

alert(typeof "hello");

That said, a (possibly) more useful trick to examine objects is to use Object.prototype.toString.call() and look at the result:

var t = Object.prototype.toString.call(itIsAMystery);

That will give you a string like [object Foo] with "Foo" being the constructor (I think) the interesting part. For "native" types (like Date or String) you get back that constructor name.

like image 44
Pointy Avatar answered Oct 23 '22 17:10

Pointy


In my experience, the main problem with typeof comes from distinguishing between arrays, objects, and nulls (all return "object").

To do this, I first check typeof then I check the null case or the "object's" constructor, like this:

for (o in obj) {
    if (obj.hasOwnProperty(o)) {
        switch (typeof obj[o]) {
            case "object":
                if (obj[o] === null) {
                    //do somethign with null
                } else {
                    if (obj[o].constructor.name === "Array") {
                        //do something with an Array
                    } else {
                        //do something with an Object
                    }
                }
                break;
            case "function":
                //do something with a function
                break;
            default:
                //do something with strings, booleans, numbers
                break;
        }
    }
}
like image 5
AutoSponge Avatar answered Oct 23 '22 17:10

AutoSponge


Not all typeof returns objects.

Objects, Arrays and RegEx returns a type of object.

Function which is an object (reference type), yet returns type of function. That's an inconsistency in the language.

Another thing to note, undefined returns undefined while null returns object which is a bug in JS.

NaN (not a number) returns a type of number.

Better you keep track of all these and be aware of these strange behaviors.

For your reference here are all the type of values:

typeof "Tamal" ---> string
typeof 100 ---> number
typeof true ---> boolean
typeof false ---> boolean
typeof undefined ---> undefined
typeof function() {} ---> function
typeof Symbol() ---> symbol
typeof {name: "Tamal"} ---> object
typeof [1, 2, 3] ---> object
typeof /^/ ---> object
typeof NaN ---> number
typeof null ---> object (bug)
like image 5
Tamal Chowdhury Avatar answered Oct 23 '22 19:10

Tamal Chowdhury