A problem arises..

Suppose you have the marks of the 6 tests recently conducted in your school and would like to store them in a program to perform operations on them like sorting, calculating percentages, the mean, the median etc. What will you use to store the data? Six separate variables for storing the marks or something more interesting?

Let's consider the former. Yes you can create six different variables to hold the marks of the six tests but doing so may not be an efficient way to carry out the aformentioned tasks. In fact some tasks may be totally impractical if done using various variables.

For example consider the problem of sorting the numbers. How can you determine the least of the six variables, then the least of the rest five, then four and so on. What if you had 100 test scores and not just 6? If you think on it for a while you'll soon realise that separate variables aren't the solution to even simple cases of this problem in any way.

So what else can we use that's more interesting and efficient than this monotonous approach. Well the answer is very simple - an array!

What are arrays?

Let's start by formally defining what an array is and then understand on the key terms step-by-step.

An array is an ordered collection of data under one hood. The data is placed at indexes in the array.

An array is much like a list of values stored in memory in a structured way.

The actual values within an array are called elements whereas their respective positions are called indexes.

An index of an array is a position in the array where data is placed.

Indexes start at 0. This means the first value in an array is at index 0, the second one is at index 1 and so on.

But how can we represent arrays in JavaScript? This will be answered in detail in the next chapter but for the sake of explaining key terms regarding arrays in this chapter we'll consider the easiest way to do so.

Arrays can be represented using square brackets [] in JavaScript. The individual values go within these brackets and are separated by commas ,.

Following is an example, demonstrating an array of the marks you scored in the 6 tests.

//   index   0   1   2   3   4   5
var marks = [37, 68, 95, 66, 71, 39]; // your marks in the six tests
You will get a better view of how indexes work in the next chapter.

But why arrays?

As you just saw above, the problem of storing your marks, with the ability to perform functions on them, can easily be solved by an array.

An array not just stores data but also gives a great vector of features that can be used on that data. You can sort the data, like numbers, in ascending order or descending order. You can search for data, like names, in an array and see if a given value exists. You can replace data, add data, remove data and do so much more.

All of these functionalities can't be provided, in any way, by separate variables storing each of the bit of data individually.

Moving on

Now that we've got a basic overview of array, including their purpose and some key terms, it's time that we learn how to create arrays in JavaScript and perform useful functionalities over them.

In the next chapter we shall cover the creation of arrays in JavaScript, before moving on to explore have put it under one topic to emphasize a lot on the important details of creation that could have been missed if we had jumbled it in other chapters.