Why javascript's typeof always return "object"?

Viewed 40918

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

always for Elements or lists.

8 Answers

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)

typeof is an operator that accepts one operand, that returns a string indicating whether the operand is a primitive, a function, an object, or undeclared.

Note that typeof does not return strings that correspond to the name of the class or constructor function used to construct an object, hence your question.

typeof returns the following for the seven primitive JavaScript types:

  1. Undefined : 'undefined'
  2. Null : 'object'
  3. Boolean : 'boolean'
  4. Number : 'number'
  5. BigInt : 'bigint'
  6. Symbol : 'symbol'
  7. String : 'string'

(...and for the proposed BigDecimal primitive type, I presume it will return 'bigdecimal'.)

typeof returns the following for objects/object categories:

  1. document.all : 'undefined'
  2. functions : 'function'
  3. all other ordinary objects : 'object'

...and, finally, typeof returns 'undefined' for undeclared identifiers.

Notes

typeof returns 'object' for null, even though null is of type Null, due to an early requirement for easy interoperability with Java, a language in which null is the empty reference type. null sits at the top of every prototype chain, and can be thought of as being within the set of object-type primitives.

typeof returns 'function' for functions, and not 'object', to enable easy identification of functions (ie. callable objects) without having to expose within the language a separate isFunction or isCallable function.

typeof provides the ONLY mechanism in JavaScript to detect undeclared identifiers (see step 2a in the spec here) without throwing an error. This is useful when attempting to dynamically detect the host environment (eg. Node.js or a Web browser), where a tested-for identifier may or may not exist.

Attempting to use typeof with an identifier within a temporal dead zone throws a runtime error (ie. normal behavior).

document.all is an object in the Web platform (ie. not part of the language) for which typeof returns undefined. This anomaly is accommodated in the language specification in order to maintain compatibility with early Web browsers.

Primitive wrapper objects like new Number(1) and new Boolean(true) are treated as objects, not primitives. So, for example, typeof new Number(1) returns 'object'. Note that typeof Number(1) returns 'number' because without the new operator, these kind of constructor functions return an instance of the primitive.

typeof is an operator, not a function. Although it can be used with a syntax that looks like a bit like a function call (e.g. typeof(foo)), the right-had side of the expression is a normal unary expression: the parentheses do not delineate a function call, but an expression.

Related