Why should we use arrays?
Let's consider that we need to store the average temperature of each month of the year of the city that we live in. We could use something like the following code snippet to store this information:
const averageTempJan = 12;
const averageTempFeb = 15;
const averageTempMar = 18;
const averageTempApr = 20;
const averageTempMay = 25;
However, this is not the best approach. If we store the temperature for only one year, we could manage 12 variables. However, what if we need to store the average temperature for 50 years? Fortunately, this is why arrays were created, and we can easily represent the same information mentioned earlier as follows:
const averageTemp = [12, 15, 18, 20, 25];
// or
averageTemp[0] = 12;
averageTemp[1] = 15;
averageTemp[2] = 18;
averageTemp[3] = 20;
averageTemp[4] = 25;
We can also represent the averageTemp
array graphically:
