5

As I think, JS array is just a hash-map which accepts only integral value as a key. And the .length property just return largest index + 1.

Is this right? Is there any other differences?

2
  • Do you mean the difference between [] and {}? Commented Jul 30, 2010 at 12:22
  • Nope. Not about syntax. I'm talking about behavior and implementation. Commented Jul 30, 2010 at 12:47

4 Answers 4

3

You are wrong; arrays can have any keys you want.

Also, they inherit the Array prototype.

Sign up to request clarification or add additional context in comments.

3 Comments

Well I would not say that arrays can have any keys. I think integer keys are treated in a special way, whereas if you use another key, like a string, you just make use of the fact that an array is also just an object. This is reflected with Array.length as the OP already said.
@Felix: The only visible special treatment of integer keys is the length property. Although implementations may optimize usage of integer keys, such optimizations must have no side-effects.
So, it seems to be safe to treat Array as a special case of hash-map.
1

The difference is:

Object.prototype.toString.call([]); // [object Array]
Object.prototype.toString.call({}); // [object Object]

Edit:

Also, have a look at this section from ECMAScript specifications as it precisely explains what an Array is: http://bclary.com/2004/11/07/#a-15.4

1 Comment

Just the information what I've been found. Now I can treat Array as just a has-table, not a C style array. Thanks.
1

A JavaScript Array also inherits from Object, so it will get all the capabilities of an object. JavaScript Arrays have additional functionality though:

var myA = ['foo', 'bar', 'baz'];
var myO = {0: 'foo', 1: 'bar', 2: 'baz'};

// these both give us "foo":
console.log(myA[0]);
console.log(myO[0]);

// array has additional methods, though:
console.log(myA.pop());
console.log(myO.pop()); // <- error

While you can add integer properties to regular Objects and add non-integer properties to Arrays, this won't give an Object the special properties and methods that Array has, and Array's special functionality only applies to its integer-keyed properties.

A good reference to all the extra properties that Arrays inherit is the Mozilla Developer Center article on Array. Make sure you pay attention to the little "non-standard" and "Requires JavaScript 1.x" notes if you want to maintain cross-browser compatibility.

1 Comment

myO.pop = function() { return "ne ne ne ne, I can pop too!" }; - here you go, an object (not array) with perfectly working .pop(). Your code doesn't prove anything.
0

Array objects can have any property that an object can have. The only special property is the "length" property that is (potentially) updated when you set an "array index" property, and that can also be used to remove array elements if set to a lower value than its current.

"Array indices" are strings (all object properties are) that is the canonical decimal representation of an unsigned integer in the range 0..2^32-2 (i.e., "0" to "4294967294"). The limit is one below the maximal value of a 32-bit unsigned value because the length field value is then always an unsigned 32-bit integer value.

Array objects also inherit from Array.prototype (but you can make other objects that do that too, if you want) and their internal class is "Array".

I.e, in practice, the only difference between an Array and a plain Object instance is the "magical length property". If you don't need that for anything, you should just use an object.

1 Comment

myObject.length = function() { return 12 }; - cool, I'm an array now. Again - your post does not prove anything and what you say is simply wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.