javascript /

Description compares a value with one or multiple type definitions and returns true if the value's type matches one definition.

The comparsion of is strict and tries to return the best matching type result only. (for example, returns false for a DOM node with cmptype "object", although a node is an object.

Requirements requires some JavaScript 1.6+ array methods:

For old browsers (like IE8 and lower) a polyfill is needed.

Code & Downloads


Boolean result = testvalue, String cmptype …);
Boolean result =[String cmptype …], Mixed testvalue);

where each cmptype is one of these strings:

"string" a string primitive "number" a number primitive
"object" a [custom] object "null" value null
"nan" value NaN "function" a function
"undefined" value undefined "boolean" a boolean primitive
"array" an Array "node" a DOM node element
"text" a DOM textnode "list" a DOM HTMLCollection or NodeList
"date" a date object "regexp" a RexExp object
"int" an integer (implies number) "float" a float (implies number)
"infinity" value Infinity

cmptype is case insensitive.

Some additional values for cmptype are accepted, e.g., 'math'); // returns true if you're lucky

but it is not recommended to test host objects (built-ins) against single specific types.


var abc = 'Hello!'; console.log(, 'string')); // truevar abc = [1,2,3]; console.log(, 'array')); // truevar abc = null; console.log(, 'object')); // false

It is possible to test multiple types:

if (, 'string', 'number', 'array')) { /* … */ }

is similar to

if (typeof myVar === 'string' || typeof myVar === 'number' || Array.isArray(myVar)) { /* … */ }


Any kind of shortcut can be created using Function.prototype bind():

var isObject =['object']); var d = {}; console.log(isObject(d)); // truevar isNullOrUndef =['undefined', 'null']); var d; console.log(isNullOrUndef(d)); // true var e = null; console.log(isNullOrUndef(e)); // true

First parameter of .bind() MUST be an array of cmptype strings


dynamic test table

table with test results goes here. javascript is required.

There should be just one true sign per row except some numeric values, which should be both number and either int or float (slightly darker blue).

true Function returned true.Okay
false Function returned false.Okay
true Function returned true; should have been false.Error
false function returned false; should have been true.Error