Your if
statement won't throw because you've made that assumption, because anything that passes your first check will support your second, even if there is no constructor
property (you'll just get undefined
), and in fact even if it's not an object (it'll get temporarily promoted). So in that sense yes, you can happily do what you're doing — unless you could be dealing with an array from another window, in which case the === Array
will fail (as dandavis pointed out) because different windows have different instances of the Array
constructor.
Note that this is not the same as every object having a constructor
property. It's just that if the object doesn't, you'll get undefined
, rather than an error. (You can get an object with no constructor
property by creating an object with no prototype: var obj = Object.create(null);
or by using an object whose prototype is null
as the object's prototype.)
However, somewhat tangentially, I wouldn't use that mechanism to check whether something is an array. In any modern environment, I'd use Array.isArray
; and I'd shim/polyfill it on older environments. This also has the happy effect of working with arrays from other windows.