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?
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?
You are wrong; arrays can have any keys you want.
Also, they inherit the Array
prototype.
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.
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
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.