What is the typeof
funtion?
The typeof operator returns a string indicating the type of the operand.
To check variable type(whether it is array, object, function etc.).
typeof
is actually an operator, not a function, it returns you the type of the supplied operand, and returns you always[1] a string value, that can be:
"undefined"
"object"
"boolean"
"number"
"string"
"function"
It can look like a function, because it can be applied to parenthesised expressions, but the parentheses are not mandatory, you can use it without them:
typeof expression
Is worth mentioning that you can reference even an identifier that is not declared (an unresolvable reference, e.g. a variable never declared) and it will not throw a ReferenceError
, it will simply produce "undefined"
, for example:
if (typeof imUndeclared == 'undefined') {
// Ok...
}
// Vs.
if (imUndeclared === undefined) { // ReferenceError!
}
A case that can cause confusion to newcomers to the language is that the typeof
operator returns "object"
for a null
value, you should keep that in mind...
Also if you want to distinguish between Array
objects, RegExp
objects, Date
objects, etc, it isn't too useful, but there are other ways to accomplish it.
[1] With the infamous exception of JScript (IE) :), which can actually return "unknown"
!!! for some host objects, e.g. typeof new ActiveXObject("Msxml2.XMLHTTP").abort; // "unknown"
typeof
is a an unary operator (and not a function), defined by the ECMAScript standard and therefore implemented by most existing JavaScript implementations (Historically speaking, it was part of JavaScript since its version 1.1, before its ownership was handed over to ECMA International).
It is used to introspect the type of a variable by querying it (typeof expression
) and checking the return value, which is a string
object indicating the type with lossy precision (see below).
Usage
As per the ECMAScript 5 Standard (ECMA-262, p. 71), it will return the following string values when tested with these given parameters of the following types:
Undefined
--> "undefined"Null
--> "object"Boolean
--> "boolean"Number
--> "number"String
--> "string"Object
--> it depends:- native and does not implement
Call
--> "object" - native and does implement
Call
--> "function" - host and does not implement
Call
--> Implementation-defined except may not be "undefined", "boolean", "number", or "string"
- native and does not implement
For most implementations, typeof
will also return:
Array
--> "object"Function
--> "object" (see aboveObject
's 2nd and 3rd case...)
(Note that types are capitalized, whereas their string identifier as returned by typeof
are not)
Caveats
- As mentioned above, the type detection with
typeof
is lossy, typeof
, as of ECMAScript 5, still doesn't supportArray
(which is identified as "object" in most cases. (see Douglas Crockford's Remedial JavaScript)- Detection of
Function
andArray
varies
Additional References
- Wikipedia's ECMAScript Syntax page will also provide usage examples of
typeof
. - Mozilla's Developer Center's JavaScript Reference for typeof