Skip to content

Instantly share code, notes, and snippets.

@adamrobbie
Created June 27, 2012 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamrobbie/3004432 to your computer and use it in GitHub Desktop.
Save adamrobbie/3004432 to your computer and use it in GitHub Desktop.
Utility method to get class of any JS object
function getClass(obj) {
if (typeof obj === "undefined")
return "undefined";
if (obj === null)
return "null";
return Object.prototype.toString.call(obj)
.match(/^\[object\s(.*)\]$/)[1];
}
getClass("") === "String";
getClass(true) === "Boolean";
getClass(0) === "Number";
getClass([]) === "Array";
getClass({}) === "Object";
getClass(null) === "null";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment