Type Detection

A common JavaScript Interview question is:

“What are the object types in JavaScript?”

The real answer is that everything in JavaScript is an Object. However, that is not the whole truth, as JavaScript has 7 meta-types: number, string, boolean, function, object, null, undefined.

Since JavaScript is not a strongly typed language, you may find it useful to manage object types yourself. For my projects, I always manage object types as it allows me to bulletproof my JavaScript functions and prevent other developers from misusing the code I share. Also, AJAX applications often return bad data because a lot can go wrong with the request and server-side code execution.

Below is a list of functions I previously used to detect object types:

Example 1: isAlien

/* True when o is not a member of a function. */
function isAlien(o) {return isObject(o) && ! isFunction(o.constructor);}

Example 2: isArray

/* True when o is a native JavaScript array */
function isArray(o) {return isObject(o) && o.constructor == Array;} 

Example 3: isBoolean

/* True when o is the meta-type boolean */
function isBoolean(o) {return boolean === typeof o;} 

Example 4: isDate

/* True when o is an object having the getMonth method */
function isDate(o) {return isObject(o) && o.getMonth;} 

Example 5: isDomElement

/* True when o is set and has children nodes or a node type */
function isDomElement(o) {return o && ("undefined" !== typeof o.childNodes || o.nodeType);} 

Example 6: isEvent

/* True when o is set, the native JavaScript event is defined, and o has an event phase */
function isEvent(o){return o && "undefined" != typeof Event && o.eventPhase;} 

Example 7: isNull

/* True when o is exactly equal to null */
function isNull(o) {return null === o;} 

Example 8: isFunction

/* True when o is the meta-type function */
function isFunction(o) {return function == typeof o;} 

Example 9: isNumber

/* True when o is the meta-type number and is finite */
function isNumber(o) {return "number" == typeof o && isFinite(o);} 

Example 10: isObject

/* True when o is the meta-type object or function */
function isObject(o) {return (o && "object" == typeof o) || isFunction(o);} 

Example 11: isSet

/* True when o has any value, including 0 and false, both of which are normally falsy */
function isSet(o) {return ((o || false === o || 0 === o) && ! isNull(o) &&  !== o && ! isUndefined(o));} 

Example 12: isString

/* True when o is of the meta-type string */
function isString(o) {return string == typeof o;} 

Example 13: isUndefined

/* True when o is of the meta-type undefined */
function isUndefined(o) {return undefined == typeof o;} 

Example 14: isEmpty

/* True when o does not have any member values set that are not functions */
function isEmpty(o) {
	var i, v;
	if (isObject(o)) {
		for (i in o) {
			v = o[i];
			if (! isUndefined(v) && ! isFunction(v)) {
				return false;
	return true;


I no longer use these methods as I prefer to use the YUI library as the backbone of my JavaScript code. Similar methods are available on YUI Lang. And you can always extend the YAHOO.lang object with extra type detection methods.

most of these functions are not bulletproof and can be fooled. For example:

var fakeDate = {
	getMonth: 1234
isDate(fakeDate);  /* this will be true */

I have not found a good way to prevent this type of abuse, except through intelligent coding. You should never use reserved names or those names used by other native JavaScript objects, such as date and array, when writing application objects.