1. Technology

JavaScript Arrays That Are Not Arrays


There are a number of different elements built into JavaScript which at first glance may appear to be arrays.

The arguments associated with a function call appears at first glance to be an array. It however doesn't have any of the methods that you might normally associate with an array apart from using length to hold the number of parameters passed.

Similarly the nodelists returned by DOM calls such as getElementsByTagName and getElementsByName also have a length but none of the other regular array properties and methods.

In fact JavaScript itself does not support true arrays as such and the Array object itself is in fact just another object no different from any other object except for the particular properties and methods that it has available.

This means that arrays in JavaScript behave differently from the way you would expect if you are familiar with the way arrays work in other languages. There is no need to define a length for an array before using it in JavaScript the way it is for other languages since as a JavaScript array is just another object you can change the length of the array at any time simply by creating an additional entry (which doesn't even need to be added directly after the last entry already in the array - you can leave as big a gap between entries as you like).

Arrays in JavaScript are in fact just a specialised type of object that supports a number of properties and methods that are normally associated with arrays. Because those methods are operating on an object rather than a real array their processing is less efficient that the corresponding code to operate on a real array would be however that isn't going to impact directly on your use of arrays.

The ways in which arrays are implemented in JavaScript that will impact on how you use them are those situations where you would normally expect arrays and objects to behave differently. Because a JavaScript array is just another type of object a JavaScript array behaves far more like an object than arrays do in other languages. In many cases you can substitute the object notation for the array notation in JavaScript (or vice versa) and have code that still does basically the same thing.

An associative array in JavaScript is in fact no different from a regular object where the names of the entries in the associative array are just the property names within the object. So x['y'] and x.y are just two different ways of referencing the same property. The array notation here provides the option of substituting a variable for the property name so as to be able to iterate over the properties of the object rather than having to hard code each into your script.

An ordered array in JavaScript simply substitutes numbers for the property names. The array notation x[15] = 'a' is simply an alternative to the object notation {'15':'a'}.

The only real difference between x = {} and x = [] is that the latter form creates an object that has the additional methods that are normally associated with an ordered array in addition to the regular object methods. That those extra methods are created doesn't mean you have to use them and so anything you can do with an object in JavaScript you can also do with an array.

This interchangeability of objects and arrays in JavaScript means both that your arrays may not always behave exactly as you expect and also means that you can swap back and forth between the two notations depending on which provides the easier way of coding what you want/ There are both advantages and disadvantages to JavaScript's approach to arrays, the important thing to remember is that Arrays in JavaScript are implemented as objects and do not actually work the same way as arrays do in other programming languages even though the methods that array objects can use may make it look that way.

  1. About.com
  2. Technology
  3. JavaScript
  4. Problem Solving
  5. JavaScript Arrays That Are Not Arrays

©2014 About.com. All rights reserved.