JavaScript data type detection

Sometimes it is really useful to know the datatypes, especially when it comes to iterating arguments passed to a function or accepting one argument of variable types, as we are going to see in some practical examples to come.

As we know data types in JavaScript are not strict, they are the loosest ever and in the previously noted cases we have to be strict to them. A bit of discipline never hurt anyone.
Detecting data types can be performed with typeof which can tell us if the data is a: boolean, number, string, function or object. Not very precise if you ask me. And there a number of cases where typeof fails, for instance if we initialize string object new String() it will return type object, or typeof [] returns object too. So we need to fall back to some other method where typeof fails, we can detect object types with instanceof or we can fall back to the good old-fashioned duck typing, looking for the special object properties, like specific functions or length property and deciding what type it is.

As the good man Juriy Zaytsev, otherwise known as “kangax”, describes on his blog
both of the noted fallbacks fail for simple logical reasons. I admit that it is an awful thing being a perfectionist and in the same time developer in an dynamic and functional language such is JavaScript so I agree that perfection kills.

Anyway we have to choose a way to detect type and I went with duck typing, which is not very professional, but it does it’s job as long as you don’t pass objects with the same combination of properties.

//Thanks to <;
__getClass = function(val) {
var whatis = function(val) {
if(val === undefined)
return 'undefined';
if(val === null)
return 'null';
var type = typeof val;
if(type === 'object') {
type = __getClass(val).toLowerCase();
if(type === 'number') {
if(val.toString().indexOf('.') > 0)
return 'float';
return 'integer';
return type;
var isSet = function(val) {
return (val !== undefined) && (val !== null);
var isEmpty = function(obj) {
if (obj.length && obj.length > 0)
return false;
for (var key in obj) {
if (, key))
return false;
return true;
var is = function(obj, type) {
if (type === 'number')
return isNumber(obj);
if (whatis(obj) === type)
return true;
if (type === 'empty')
return isEmpty(obj);
if (type === 'set')
return isSet(obj);
return false;
var isArray = function(val) {
return is(val, 'array');
var isNumber = function(val) {
if (isNaN(val))
return false;
var type = typeof val;
if (type === 'object')
type = __getClass(val).toLowerCase();
return type === 'number';
var isInt = function(val) {
return is(val, 'integer');
var isFloat = function(val) {
return is(val, 'float');
var isString = function(val) {
return is(val,'string');
var isObject = function(val) {
return is(val, 'object');
var isFunction = function(val) {
return is(val, 'function');
var isDate = function(val) {
return is(val,'date');
var isBool = function(val) {
return is(val, 'boolean');
var isRegExp = function(val) {
return is(val, 'regexp');

view raw
hosted with ❤ by GitHub

I agree that this is far from a perfect solution but it is a start. With the method whatis we expended the set of types to: boolean, float, int, string, array, date, function and object which was enough for my personal needs. The provided methods are sugar methods for a method is that is used to assert the type.

If you have any suggestions or improvements please leave a comment. I would appreciate any constructive criticism! :)


  1. […] So I have developed a simple function in JavaScript that does function overloading in a more beautiful way which is pretty to read and manage. *Note: whatis() function is described in the following post […]

  2. var zomg = undefined;

    zomg == undefined // true
    zomg == null // true

    zomg = null;

    zomg == undefined // true
    zomg == null // true


    1. zomg === undefined //true
      zomg === null //false


  3. […] Note that “whatis” function for data type detection is described in this post: […]

  4. […] The provided solution is using a sligntly modified (for performance) JavaScript implementation of a CRC32 hashing algorithm written by Andrea Ercolino  and can be found here: Other external functions are orderProperties (self explanatory), and functions described in older posts: JavaScript deep object comparison and JavaScipt data type detection. […]

  5. […] For some time I needed to use objects as a hash table (associative array) key in JavaScript, and for all that time I skillfully evaded to wrap my head around it. JavaScript objects (hash tables/associative arrays) are unordered data sets which means that the property (key) order isn’t guaranteed. I believed that for(var i in obj) will always execute the same way and that it will be the order properties were added into the object. Also I knew that JSON.stringify() uses the same way of iteration. I tinkered with hash tables last two days and made my implementation in JavaScript, and it used a function I’ve created that orderes the properties by creating a new object and inserting the properties in the correct order. It an array with keys, sorts them alphabetically or via provided function which is not obligatory, and then it fills the empty object with properties in new order. Now when I think of it, I forgot to cover a case where a property holds an array and in that array there is an object. So I fixed that too. Helper functions in both examples described in […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: