0

I was wondering if it is safe to assume that in JavaScript a variable will always have a "constructor" property if its value is not "null" or "undefined".

I ran into a situation where I had to check if a variable is defined and that if it is an array and if so check if its length is > 0:

if(variable && variable.constructor === Array && variable.length > 0) {
  ...loop through the array
}

Am I right to assume that variable will always have a constructor if it is not "undefined" or "null"?

  • 1
    no, Arrays from other contexts (read: tabs/frames) will NOT have a constructor === `window.Array`. – dandavis Jan 11 '17 at 18:36
  • @dandavis: Good point! If there's any cross-window stuff going on, you can't trust that... – T.J. Crowder Jan 11 '17 at 18:36
  • @dandavis: thanks for that useful tip! – codeAscetic Jan 11 '17 at 18:53
  • See also [Does every object in JS have a toString() method?](http://stackoverflow.com/q/18640776/1048572) - No. And no, the only proper way to check for an array is `Array.isArray(variable)`. – Bergi Jan 11 '17 at 18:54

2 Answers2

0

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.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

undefined or null do not have constructors and will error if you try to get it. Just try it out on Ctrl+Shift+I, and play around with it. Though, I doubt your if statement would cause much trouble, and it'd most likely error on pratical use.

You do NOT need to check length or anything, only see if it exists (or if you want specifically array, you could check that aswell).

Cernodile
  • 139
  • 7
  • *"`undefined` or `null` do not have constructors and will error if you try to get it."* The OP is ruling those out earlier in in the `if`. And the issue really isn't that they don't have constructors, but rather than you can't attempt to retrieve properties from them. – T.J. Crowder Jan 11 '17 at 18:36