Intro to Arrays
What is an array?
An array is not limited to only storing primitive data types and has the ability to store multiple objects and even arrays within itself. This is more of an advanced concept, but if you are intrigued after this crash course, I empower you to simply google array of arrays and explore.
But how do we make one and what does it look like?
Simple as that. We have declared our variable of jury that is an empty array. Let us fill in our jurors because this would be a real quick case dismissed without the jury. For this example, we will be using the classic 12 Angry men.
That is a little long and for longer arrays I prefer to do this for better readability,
We have now added our jurors to our array of jury. You will notice that each juror’s name is represented as a string, each string except the last is followed by a comma as well as a space. This is the correct way to declare different objects within your array. A quick console log will simply return that array.
Now you might be thinking great we have our array of 12 angry jurors, I want to know who the last juror is. So, to do this you are probably thinking we simply need to console log our juror at position 12 in our array like so.
Well I’m sorry to be the bearer of bad news but…
Why did this happen?
Well, this is because arrays positions run off an index, and hint hint, our index does not start at 1. It starts at 0. Think of a hotel if you will, technically when you walk in and go to the check-in desk you would be on the first floor. This is not the case, you are technically on the ground floor, zero if you will. You have gotten your key for room 105, which happens to be up the stairs one floor, to the first floor. The first floor just happens to be on the second story, and the same holds true with arrays.
Lets take a look at this example where we will try and grab our first juror,
Awesome, we were able to grab him because we console logged our jury at the ground floor, zero. If you have already done the math, and we still wanted to know who our twelfth juror is. We would need to index our array of jury at 11,
I would like to thank you for reading along and hope that you have learned something new! Keep an eye out for more articles in the future!