Skip to content

Instantly share code, notes, and snippets.

@sagiavinash
Created April 28, 2015 17:07
Show Gist options
  • Save sagiavinash/428519259b282e710fe8 to your computer and use it in GitHub Desktop.
Save sagiavinash/428519259b282e710fe8 to your computer and use it in GitHub Desktop.
Get object class
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