64

How can I check if my javascript object is of a certain type.

var SomeObject = function() { }
var s1 = new SomeObject();

In the case above typeof s1 will return "object". That's not very helpful. Is there some way to check if s1 is of type SomeObject ?

5 Answers 5

92

Yes, using instanceof (MDN link | spec link):

if (s1 instanceof SomeObject) { ... }
3
  • Indeed, it goes back to the earliest JavaScript version in Netscape 2.0. (Another reason not to link to ECMA-262 is that it's staggeringly unclear and unreadable, even by standards-document standards!)
    – bobince
    Sep 28, 2009 at 13:18
  • @bobince: It does take some getting used to the style. :-) Sep 28, 2009 at 14:44
  • @bobince: Looking at the ES6 spec...well...let's just say I'm now actively pining for the glory days of clarity in the ES5 spec. :-) Feb 6, 2015 at 15:15
22

Whatever you do, avoid obj.constructor.name or any string version of the constructor. That works great until you uglify/minify your code, then it all breaks since the constructor gets renamed to something obscure (ex: 'n') and your code will still do this and never match:

// Note: when uglified, the constructor may be renamed to 'n' (or whatever),
// which breaks this code since the strings are left alone.
if (obj.constructor.name === 'SomeObject') {}

Note:

// Even if uglified/minified, this will work since SomeObject will
// universally be changed to something like 'n'.
if (obj instanceof SomeObject) {}

(BTW, I need higher reputation to comment on the other worthy answers here)

1
  • 3
    See this link. The constructor property is another interesting way to go. Just continue to avoid the string approaches.
    – AAron
    Jul 24, 2016 at 1:47
4

Idea stolen from http://phpjs.org/functions/get_class/, posted by SeanJA. Ripped down to work with objects only and without need for a regular expression:

function GetInstanceType(obj)
{
    var str = obj.constructor.toString();
    return str.substring(9, str.indexOf("("));
}

function Foo() {
    this.abc = 123;
}

// will print "Foo"
GetInstanceType(new Foo());

I just learned an easier way to extract the function name from the constructor:

obj.constructor.name
2
  • Fails with uglifiers
    – s3c
    Mar 29, 2022 at 7:41
  • @s3c What fails? If you use uglifiers/tersers, usually what fails is hardcoded "name comparisons", because an uglifier/terser is shortening all names and the string comparisons fail. In that case, compare with the actual class.
    – kungfooman
    Mar 30, 2022 at 7:47
2

You could also take a look at the way that they do it in php.js:

http://phpjs.org/functions/get_class:409

1
  • Although a link can be useful, it would have been better if the information sere included in the answer.
    – trincot
    Apr 4, 2019 at 18:13
1

While instanceof is a correct answer it sure is ugly syntax. I offer that if you are creating custom objects you can add your own property for type and check against that like so...

var Car = function(){
    this.type = Object.defineProperty(this, "type", {value:"Car"});
}

This would create an immutable property called type that lives with the object. If you were using the Class syntax you could make it static as well.

... somewhere later ...

function addCar(car){
    if (car.type != "Car"){
        throw Error("invalid type for car");
    }

...

I think this solution is easy to implement, more intuitive, and thus easier for others to use and maintain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.