Skip to content

Instantly share code, notes, and snippets.

@melvynhills
Created September 19, 2011 11:37
Show Gist options
  • Save melvynhills/1226337 to your computer and use it in GitHub Desktop.
Save melvynhills/1226337 to your computer and use it in GitHub Desktop.
JavaScript getClass function
function getClass(object) {
return Object.prototype.toString.call(object).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
}
var o = {'hello':'world'};
console.log(typeof o, getClass(o)); // object, object
var a = ['hello','world'];
console.log(typeof a, getClass(a)); // object, array
var d = new Date("October 13, 1975 11:13:00");
console.log(typeof d, getClass(d)); // object, date
var n = 56788;
console.log(typeof n, getClass(n)); // number, number
var s = "hello world";
console.log(typeof s, getClass(s)); // string, string
@melvynhills
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment