Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a JavaScript Array?

Tags:

javascript

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?

like image 736
Phillip Senn Avatar asked Oct 19 '25 16:10

Phillip Senn


2 Answers

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';

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

Here is a whole array tutorial

like image 26
Naftali Avatar answered Oct 22 '25 05:10

Naftali