What is an array?
An array is where you can store multiple values: typically series of numbers or strings.
For example, in Javascript, say that you have an array called x.
let x = [10, 15, 5, 7];
This array contains four numbers.
A way to think of an array is to imagine a cabinet with multiple drawers. Imagine that the first drawer of this cabinet has a sticky note that says “0”. Once you open that drawer with the note “0”, inside, say you find a value of “10”.
A visual illustration below:
Imagine that the second drawer of this cabinet has a sticky note that says “1”. Once you open that drawer, you find a value of “15” there.
In essence, this array called x has four drawers, each drawer with the sticky note of 0, 1, 2, and 3. This sticky note (attached to the front of the drawer) is also called an index.
If I want to log the value stored in each drawer, I can do so by doing this:
console.log(x[0]); //this prints 10
console.log(x[1]); //this prints 15
console.log(x[2]); //this prints 5
console.log(x[3]); //this prints 7
I could also update each value in the array by doing this
x[0] = 15; //this updates the value in x[0] to be 15
x[1] = 7; //this updates the value in x[1] to be 7
x[2] = 14; //this updates the value in x[2] to be 14
x[3] = 25; //this updates the value in x[3] to be 25
If I was to log x now, it would be
console.log(x); //would log [15, 7, 14, 25]
Adding values to the array
If I want to add a new drawer to this cabinet, I can do so by calling a push method:
let x = [10,5,3,1];
x.push(15); // this adds a new value of 15 to the end of the array
As there were already drawers with index of 0, 1, 2, and 3, this last items gets added as the index of 4.
For example
console.log(x[4]); //this would log 15
I can continue to push new items to the array. For example
x.push(77); //this pushes a new number to the end of the array
x.push(99); //this pushes a new number to the end of the array
Removing a value from the array
You can also remove a value from the array. A common function to do this is by using a pop method.
For example, consider the following code
let x = [5,3,7,13];
x.pop(); //this removes the last value in x
console.log(x); //logs [5,3,7]
I can perform pop() several times. For example,
let x = [5,3,7,13];
x.pop(); // removes the last value 13 from x
x.pop(); // removes the last value 7 from x
x.push(77); // pushes a new value 77 to the end of x
x.push(99); // pushes a new value 99 to the end of x
console.log(x); // logs [5,3,77,99]
Iterating through an array
I can use a for loop to loop through an array. For example
for(let i=0; i<x.length; i++){
console.log(x[i]);
}
Using a for loop, we can have i start from 0 and have it increment by 1 as long as i is less than x.length. As it goes through the loop, we log what’s stored in x[i].
Having an array inside an array
Imagine a cabinet x with 4 drawers. Imagine that when you open the first drawer (e.g. x[0]), inside, you found another cabinet with 3 drawers! How would this look?
let x = [ [1,2,3], 10, 3, 5];
Way to visualize above is that x has 4 drawers. Inside the first drawer is another cabinet! For example
console.log(x[0][0]); // this would log 1
console.log(x[0][1]); // this would log 2
console.log(x[0][2]); // this would log 3
Want more practice?
Understanding how an array works is very important, and is considered one of the four core-building blocks of software. If you want more practice, go to www.hackerhero.com and work on different challenges.