Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Really get an object's type in JavaScript
function toType(obj) {
var systype = ({})\s([a-zA-Z]+)/)[1];
if (systype === "Object") {
* See if this is a custom user type, a la:
* function Foo() {}
* foo = new Foo();
* toType(foo); // returns "Foo"
* toType(Foo); // returns "Function"
var usertype = obj.constructor.toString().match(/function (.*?)\(/)[1];
return usertype;
} else {
return systype;

This comment has been minimized.

Copy link

@theohogberg theohogberg commented Sep 4, 2015

what about

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.