1

I have had discussions with seasoned programmers who do not think arrays are objects; they think that objects and arrays are of separate types of the data structure category. But, as far as I understand, objects and arrays are of the same type.

If you were to use the unary typeof operator to evaluate a binding storing an array, the JavaScript interpreter will return the string "object." Moreover, you can use the Object.keys() method on a binding containing an array to get the property names of the array object.

When studying programming, I see authors use the phrase "objects and arrays," as if these were separate entities, which adds to the confusion. I go to W3Schools, and in the section "Data Types," they list arrays and objects as if they are not of the same data type.

As far as I understand it, objects and arrays are the same, except array objects are structured different than objects delimited by braces. Perhaps I'm missing something and can be enlightened.

Are arrays objects? If so, shouldn't they be listed as the same type, and shouldn't the phrase be "arrays and other objects"? If not, why not?

(Edit: I know the question of if objects and arrays are the same has been asked, but I was also asking if there should be a change in how arrays and objects are discussed, so people don't think that arrays are not objects, since programming is not the easiest subject to digest.)

Custer
  • 119
  • 1
  • 8
  • 3
    *with seasoned programmers who do not think arrays are objects* — ummmm ... – Pointy Mar 13 '19 at 01:43
  • 2
    Two general classes of values in JavaScript: *primitives* and *objects*. Arrays are most definitely objects. They're somewhat special, but they're objects. Note that we're talking about JavaScript here; other languages are different in various ways. – Pointy Mar 13 '19 at 01:45
  • I think there are some seasoned programmers that don't pay attention to the definition; it's about just getting the program to work, not worrying about semantics. – Custer Mar 13 '19 at 01:46
  • Well you can get pretty confused in JavaScript if you don't realize that arrays are objects; they totally are. Array element references use precisely the same syntax as object element references. – Pointy Mar 13 '19 at 01:47
  • Welcome to Stack Overflow! This is a completely valid question; you may feel free to ignore the downvote. I suspect it sounded a bit like an opinion-based question the way you first phrased it. I edited it a bit, and made the title a better summary of what you are actually asking, which is a very clear, direct, on-topic programming question. However, it has already been asked and answered here on Stack Overflow, so I've marked this as a duplicate. – Cody Gray - on strike Mar 13 '19 at 01:50
  • Hey, Cody. Thanks for the welcome. :) I know the question of if objects and arrays are the same has been asked, but I was also asking if there should be a change in how arrays and objects are discussed, so people don't think that arrays are not objects, since programming is not the easiest subject to digest. – Custer Mar 13 '19 at 01:53

1 Answers1

3

Arrays are indeed a type of object.

console.log([] instanceof Object);

I go to W3Schools, and in the section "Data Types," they list arrays and objects as if they are not of the same data type

W3Schools is not a particularly trustworthy source. That said, when discussing organization of data, it's pretty common to talk about arrays (an ordered collection of values) as distinct from objects (a usually-unordered collection of key-value pairs), despite the fact that one is a subtype of the other, because the fact that arrays inherit from Object.prototype often isn't something one needs to consider when organizing / sorting through data.

If one tries to use any of the Object methods on an array, it's certainly essential to remember that Array inherits from Object, but usually that's not necessary - often, one will be only be using array methods instead (like forEach, find, includes, etc).

Snow
  • 3,820
  • 3
  • 13
  • 39
  • Just to be clear, are arrays objects in all programming languages? – Custer Mar 13 '19 at 01:57
  • 1
    Definitely not - Javascript just happens to be one of the languages for which the idea of an "Array" is a subtype of the idea of an "Object". I'm certain many other languages are different. – Snow Mar 13 '19 at 02:04
  • Thanks again for taking the time to answer my question. :) – Custer Mar 13 '19 at 02:06