Submodule 7.1: Arrays

  • Definitions
  • Accessing an array
  • Array methods


What is an array? 

Arrays are JavaScript’s simplest way of ordered collections of data. They are used to store multiple values in a single variable.

You can think array as a group of "boxes". Each box has a unique identity, which is called an index. The index of the first box is 0.


Creating an array 

This is how you can create a new array which contains 5 "boxes":

var colors = ["red", "green", "blue", "cyan" , "black"];

Also, you can first create an empty array and then fill it with various elements.

var myArray = [];
myArray[0] = "cat";
myArray[1] = 1;
myArray[2] = true;
myArray[3] = ["a", "b"];

Arrays can contain anything! This means that they can contain any data type (string, number, boolean), even an array!