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 http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/
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.

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! :)

Advertisements

6 comments

  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

      :P

  3. […] Note that “whatis” function for data type detection is described in this post: https://stamat.wordpress.com/2013/04/09/javascript-data-type-detection/ […]

  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: http://noteslog.com/post/crc32-for-javascript/ 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 https://stamat.wordpress.com/javascript-quickly-find-very-large-objects-in-a-large-array/, 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 https://stamat.wordpress.com/2013/04/09/javascript-data-type-detection/ […]

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: