Friday, July 19, 2013

JavaScript type checks

JavaScript type checks have slight complexity. This and the reason that your code should be consistent through your project(s) make it necessary that type checks are part of your javascript programming guidelines.
I am using the recommodations from the jQuery project.

They say:

String
 typeof object === "string"  
Number
 typeof object === "number"  
Boolean
 typeof object === "boolean"  
Object
 typeof object === "object"  
Plain Object
 jQuery.isPlainObject(object)  
Function
 jQuery.isFunction(object)  
Array
 jQuery.isArray(object)  
Element
 object.nodeType  
null
 object === null
null or undefined
 object == null
undefined
     Global Variables
 typeof variable === "undefined"
     Local Variables
 variable === undefined
     Properties
 object.prop === undefined