views:

103

answers:

4

What is the typeof funtion?

+1  A: 

The typeof operator returns a string indicating the type of the operand.

Darin Dimitrov
A: 

To check variable type(whether it is array, object, function etc.).

Ilian Iliev
+10  A: 

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"

CMS
+3  A: 

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"

For most implementations, typeof will also return:

  • Array --> "object"
  • Function --> "object" (see above Object'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 support Array (which is identified as "object" in most cases. (see Douglas Crockford's Remedial JavaScript)
  • Detection of Function and Array varies

Additional References

haylem
There is a function, `dltypeof()` that addresses most of these issues: http://www.webreference.com/dhtml/column68/ Note that an additional check for the presence of push is required near the end to catch arrays that weren't constructed with `new Array()`.
Stan Rogers
@Stan Rogers: well, first browser I tried (Google Chrome 6) doesn't support it, so I wouldn't advise to use that. For array detection, Dojo's `isArray()` and `isArrayLike()` is good enough for me, if I'm in an environment where using a lib is an option. Otherwise, just rolling out your own implementation is fine.
haylem
@Stan Rogers: OK I assume you were mentioning an addition to the JS spec, but I noticed it's just a library, apparently. Then in that case I guess it's up to you, but I'd stick to the standard and a more full-blown framework if I need that, and use dltypeof.js only if I need a similar functionality in a small script.
haylem
Odd -- it works in my copies of Chrome 6 (and Firefox, Safari, Opera and IE 6, 7 and 8) across five machines. And I've yet to find a project that warranted the Dojo tax (or jQuery) -- I consider it a major failure if an entire page with all resources is that heavy. But I have to deal with remote dialup and satellite (the Great White North is still pretty empty in a lot of places).
Stan Rogers
@Stan Rogers: actually I'm on Chrome 7.0.517.24 beta, but still. Doesn't work either for FF 3.6. Maybe you're just trying it on a page that embeds it? Sure, Dojo or jQuery is way overkill if it's just for that, but for the type of enterprise software I've been working on, using Dojo is justified and then it comes with that sort of convenience functions. Otherwise, like I said, re-implementing it or including dltypeof.js would make sense.
haylem
haylem
@Stan Rogers: Just realized... when I said the browsers don't support it, I meant that it's not included by default, of course. Not that it doesn't work if you include the function manually.
haylem
@Haylem: I probably should have been more clear about that. And the dltypeof() function did kind of lose out to jQuery, Dojo, Prototype/Scriptaculous and mootools. Timing, really -- thay all originated around the same time. Too bad, really, since it would have brought those libraries down in size. My largest standard library (minified) is roughly 10KB, but also reimplements most of Lotus Domino's Formula Language in JS (it just makes it easier to keep browser-side and server-side/Notes client code in sync). dltypeof is a big part of what keeps it small.
Stan Rogers