Submodule 7.1: Arrays
- Accessing an array
- Array methods
What is an array?
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 = "cat";
myArray = 1;
myArray = true;
myArray = ["a", "b"];
Arrays can contain anything! This means that they can contain any data type (string, number, boolean), even an array!