Arrays are Objects

JavaScript variables can be objects. Arrays are special kinds of objects, and because of this, you can have variables of different types of objects in the same Array. You can have functions and you can have arrays in an Array:

  myArray[0] = Date.now;
  myArray[1] = myFunction;
  myArray[2] = myCars;

Arrays are a special type of objects. Even though the typeof operator in JavaScript returns "object" for arrays, JavaScript arrays are best described as arrays. As you know, arrays use numbers to access its "elements". For example, person [0] will return John, when you use this declaration:

  var person = ["John", "Doe", 46];

Now, just as objects can use names to access its "members", arrays can do the same thing, as demonstrated in the example below:

HTML file: Displayed by browser:
<body>
<p id="demo"></p>
<script>
var person = {
    firstName: "John",
    lastName: "Doe",
    age:46
};
document.getElementById("demo").innerHTML = person["firstName"];
</script>
</body>


BackTable of ContentsNext