16

Is a instanceof b exactly equivalent to a.constructor === b? If not, what's the difference between the two?

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • Hope it is helping http://stackoverflow.com/q/18055980/1989472 – Rajasekhar Aug 11 '13 at 14:27
  • @Rajasekhar that couldn't be more misleading, it doesn't focus on the semantics of `.constructor` vs `instanceof`.. but rather confusions around strings and objects. – Esailija Aug 11 '13 at 14:52
  • possible duplicate of [CoffeeScript: Using instanceof vs Class.constructor.name](http://stackoverflow.com/questions/11690894/coffeescript-using-instanceof-vs-class-constructor-name) – Bergi Aug 11 '13 at 16:07
  • 3
    Check this example: `[] instanceof Object` but `[].constructor != Object` – Bergi Aug 11 '13 at 16:08

3 Answers3

22

No.

instanceof also checks for "inherited" constructors.

For more information, see the spec. (here and here)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
19

SLaks' answer is correct, but I would like to add that .constructor works on primitive types and instanceof doesn't:

"Hello World".constructor == String;    //true
"Hello World" instanceof String;    //false

The reason why the second condition is false is because "Hello World" is a primitive string and not an object so it isn't an instance of anything. In the first condition, on the other hand, when applying a method (such as constructor) to a primitive type, it gets converted to an object. So first "Hello World" gets converted to new String("Hello World") and then returns new String("Hello World").constructor which is String. This works the same way for numbers and booleans.

You can also use typeof on primitive types, but that won't work on objects:

typeof "Hello World";    //"string"
typeof new String("Hello World");    //"object"

So if you're dealing with strings, numbers or booleans and you don't need to worry about inheritance, you should use:

  • .constructor if you want to check if the variable is a string, number or boolean and it doesn't matter if it's a primitive type or an object.

    There are also other ways of doing this like typeof foo.valueOf() or (foo instanceof String || typeof foo == "string"), but .constructor is the shortest. However, the longer ways can be useful if for some reason you have classes that inherit String, Number or Boolean and you also want to check for inherited types.

  • instanceof if you want to check if the variable is a String, Number or Boolean object and not a primitive type.
  • typeof if you want to check if the type of the variable is a primitive type and not an object.
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
0

Also, if you are dealing with objects, the constructor property can be overwritten (if you write ObjectName.prototype), so it’s generally better to use the instanceof method to check the type of an object.

Or you can specify the constructor inside the prototype declaration:

ObjectName.prototype = {
  constructor: ObjectName,
  someOtherProp: ''
}
Julie
  • 1
  • 1