4

I'm trying to understand what a JavaScript array is because traditional programming languages define an array as a contiguous area of storage that can be addressed using an offset.

Now, a normal JavaScript object can be addressed as:

myObj.myProperty = "my Value";

or

myObj["myProperty"] = "my Value";

So, a JavaScript array is simply using numbers instead of names in it's addressing:

myObj[0] = "my Value";
myObj.length // === 1

A JavaScript Array also has methods, such as slice(), and join().

Q: Is what I said so far true?

2
  • 3
    @harper89: w3fools.com Commented Jun 22, 2011 at 18:57
  • 1
    Slight correction on one point: "a JavaScript array is simply using numbers instead of names in it's addressing" -- it's actually still using strings. When you type myObj[0], it gets converted to myObj['0'] Commented Jun 22, 2011 at 19:05

5 Answers 5

5

A JavaScript array is a hash object with array functions attached using Array.prototype. Put simply, this is an "Array" in JavaScript:

var x = {
    length : 3,
    '0'    : 'first',
    '1'    : 'second',
    '2'    : 'third'
};
x.__proto__ = Array.prototype;

All of the array functions only act on indexes, as you would expect, however you can also do anything to an array object that you would do to a general JS object:

ary.foo = 'bar';
Sign up to request clarification or add additional context in comments.

6 Comments

@Neal - only because it's a syntax error in JS. x['0'] works fine
@cwolves, yes i know that, but you stated that you van do the same operations as a normal object which you cannot.
@Neal - yes, you can. You can't do x.0 on a generic object either :)
@cwolves. yes but all of the indexes on an array object are usually numbers
@Neal - they're always numbers. There's nothing you can do to a generic object that you can't do an array since Array inherits from Object. The fact that dot notation doesn't work on numbers doesn't negate that, it just means that you can't access numeric properties the same way.
|
2

To a basic yes or no question: Yes all of what you said is true.

Here is a whole array tutorial

Comments

1

Javascript objects are associative arrays. Javascript has an Object called Array that has special methods for dealing with their data.

1 Comment

-1: very bad phrasing for newbies to JS. JS arrays are NOT associative arrays. When used as arrays, they act as normal indexed arrays. When used as an object they're associative. There are loads of arguments against using them as objects.
1

a good read ( that got me going at start ) Mastering Javascript Arrays

2 Comments

Thanks Ates! I'll look at it later tonight!
lol, you ment "Thanks Dementic" :D he just fixed a misspell of mine. XD
1

Any JavaScript array is an object that can use different objects* as keys, making it a hash.

*all objects different from strings will be converted to string [object Object], so they will act as the same key! (thanks to cwolves :)

3 Comments

@cwolves - any "non string" value different from null and undefined will be converted into a string because all object property names are strings!
yes, I realize that, but saying that you can use "different objects as keys" implies that this will work as expected: var x={}, y={}, z={}; x[y] = 1; x[z] = 2;, but in reality you'd get x == {'[object Object]' : 2}. And FYI, null and undefined work too :)
Note that you can define your own toString() method on your objects and then they will be usable as keys (assuming you come up with a meaningful way of providing uniqueness).

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.