Title: Misuse of the Array Object in JavaScript
Author: Alex Kirk
Published: May 18, 2006

---

# Misuse of the Array Object in JavaScript

May 18, 2006

There is a very good post about [Associative Arrays considered harmful](http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/)
by [Andrew Dupont](http://www.andrewdupont.net/about/).

The title is a bit misleading but correct. When coming accross a piece of JavaScript
like this
 `foo["test"] = 1;` there is nothing wrong about it. It’s the basic usage
scheme of assoziative arrays. Or should i rather say objects?

While in languages such as PHP arrays used like this `$foo = array("test" => 1);`
is perfectly correct.

In JavaScript
 `var foo = new Array(); foo["test"] = 1; works but does not do what
you want.

I don’t need to repeat Andrew’s really great post, but basically you should use `
Object` instead of `Array`.

`var foo = new Object(); // same as var foo = {};
 foo["test"] = 1; // same as foo.
test = 1;

Now go and read [Andrew’s post](http://www.andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/).

via [Erik Arvidsson](http://erik.eae.net/archives/2006/05/18/10.31.01/).

btw: that post lead me to [Object.prototype is verboten](http://erik.eae.net/archives/2005/06/06/22.13.54/)
which explains for me why my `for (i in myvar) {}` loops never worked correctly.
I was using [prototype.js](http://prototype.conio.net/) version < 1.4 (which messed
with Object.prototype).

javascript, array, object, prototype.js, Object.prototype

[Ajax](https://alex.kirk.at/category/code/ajax/), [Code](https://alex.kirk.at/category/code/)

Read this next

[Welcome naked!](https://alex.kirk.at/2006/04/05/welcome-naked/)

## 4 responses to “Misuse of the Array Object in JavaScript”

 1.  ![Chris Double Avatar](https://secure.gravatar.com/avatar/acf86b1abd392f1697ea374d3d0e0a89c7083624d0561211088d9c7d19a79ebb?
     s=48&d=mm&r=g)
 2.  [Chris Double](http://www.bluishcoder.co.nz)
 3.  [May 18, 2006](https://alex.kirk.at/2006/05/18/misuse-of-the-array-object-in-javascript/comment-page-1/#comment-1385)
 4.  Using ‘Object’ as an associative array can be bad too:
 5.  [http://www.bluishcoder.co.nz/2006/05/associative-arrays-and-javascript.html](http://www.bluishcoder.co.nz/2006/05/associative-arrays-and-javascript.html)
 6.  Basically naieve use of Object can return built in operations on Object (like 
     toString).
 7.  [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2006%2F05%2F18%2Fmisuse-of-the-array-object-in-javascript%2F)
 8.  ![Thomas Frank Avatar](https://secure.gravatar.com/avatar/68cb4894beecc4254f9e0ee65532dbd907b779faa94c87842eccb15ee5ce9c91?
     s=48&d=mm&r=g)
 9.  [Thomas Frank](http://www.thomasfrank.se)
 10. [July 14, 2006](https://alex.kirk.at/2006/05/18/misuse-of-the-array-object-in-javascript/comment-page-1/#comment-3503)
 11. Object.prototype is erlaubt ;-)
      [http://www.thomasfrank.se/object_prototype_is_erlaubt.html](http://www.thomasfrank.se/object_prototype_is_erlaubt.html)
 12. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2006%2F05%2F18%2Fmisuse-of-the-array-object-in-javascript%2F)
 13. [alexander kirk » Blog Archive » JavaScript Tricks And Good Programming Style](http://alex.kirk.at/2006/08/09/javascript-tricks-and-good-programming-style/)
 14. [August 9, 2006](https://alex.kirk.at/2006/05/18/misuse-of-the-array-object-in-javascript/comment-page-1/#comment-4294)
 15. […] Misuse of the Array Object in JavaScript […]
 16. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2006%2F05%2F18%2Fmisuse-of-the-array-object-in-javascript%2F)
 17. [Quotes » Blog Archive » Extending prototypes of built-in objects](http://quotes.dahandahanlang.com/index.php/2007/03/08/extending-prototypes-of-built-in-objects/)
 18. [March 8, 2007](https://alex.kirk.at/2006/05/18/misuse-of-the-array-object-in-javascript/comment-page-1/#comment-40131)
 19. […] This post was in response to a series of posts Iâ€™ve seen on blogs lately,
     such as Bluish Coder: Associative Arrays and Javascript (Chris Double), JavaScript
     â€œAssociative Arraysâ€ Considered Harmful (Andrew Dupont), Misuse of the Array
     Object in JavaScript (Alexander Kirk) and Object.prototype is verboten (Erik Arvidsson).[…]
 20. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2006%2F05%2F18%2Fmisuse-of-the-array-object-in-javascript%2F)

### Leave a Reply 󠀁[Cancel reply](https://alex.kirk.at/2006/05/18/misuse-of-the-array-object-in-javascript/?output_format=md#respond)󠁿

Only people in [my network](https://alex.kirk.at/friends/) can comment.

This site uses Akismet to reduce spam. [Learn how your comment data is processed.](https://akismet.com/privacy/)