Skip to content

Instantly share code, notes, and snippets.

@wiky
Created June 28, 2012 04:25
Show Gist options
  • Save wiky/3009060 to your computer and use it in GitHub Desktop.
Save wiky/3009060 to your computer and use it in GitHub Desktop.
check the js variable type
(function(global) {
var type = (function() {
var cache = {};
return function(obj) {
var key;
return obj === null ? 'null' // null
: obj === global ? 'global' // global
: (key = typeof obj) !== 'object' ? key // array, boolean, function, number, undefined
: obj.nodeType ? 'object' // DOM element
: cache[key = ({}).toString.call(obj)] // cached [object XXXX]
|| (cache[key] = key.slice(8, -1).toLowerCase()); // date, error, math, object, regexp
};
}());
if (typeof exports !== 'undefined') {
exports.type = type;
} else {
global.type = type;
}
}(this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment