Skip to content

Array Summary

Declaring an Array

1
2
3
// Declare an Array
let myArray;
myArray = [];

Adding Elements to an Array

1
2
// Quick assignment
myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

Adding Elements to an Array with the length property

1
2
// The value can be any data type
myArray[myArray.length] = 1234;

Adding Elements to an Array with the .push() method

1
2
// The value can be any data type
myArray.push("This is a string");

Accessing an Element in an Array

1
2
// Direct access to an element
output.innerHTML = myArray[0]; //<-- directly accessing

Looping Through an Array

1
2
3
4
5
6
// Indexed access
let index;

for (index = 0; index < myArray.length; index++) {
    myArray[index]; //<-- do something with this
}