An array is simply defined as a list of objects. The objects or items in your array can be virtually anything you want although typically, they are related to one another. Let's make use of students in a class as our example, if you want to keep track of them, make an array of the students:
var students= new Array();
students[0] = 'George';
students[1] = 'Michael';
students[2] = 'Kristine';
students[3] = "Jane';
See, there's really nothing to it, it's pretty simple to assign the elements of each variable. The example above is just one of the ways you can formulate the code. As you may already be aware of, there are many ways in which you can
create a code in the JavaScript language, creating an array is no different. Here is another more compact and less tedious example:
var students = ['George', 'Michael', 'Kristine', 'Jane'];
This works exactly the same ways as the first example but as you can see, it is more compact and is easier to read. The brackets that contain the name tell your code that you are creating an array. By writing "var students = [];" you get the same effect as writing "var students = new Array();". You may prefer one over the other so use whichever method you want. You will see that
JavaScript language is easy to understand.
Now that we have an array, what should we do with them? Before you can access the items in your array, you need to know its index. See those number in between the bracket in the first example? Those are the index of the item. Arrays in JavaScript start from 0 instead of 1, so the first item in an array is students0] and so on. Also, you should check how long your array is so you can loop through it and
perform tasks on each item.
<html>
<body>
<script type="text/javascript">
var students = ['George', 'Michael', 'Kristine', 'Jane'];
var suffixes = ['1st', '2nd', '3rd', '4th'];
for(var i=0; i
alert('The '+suffixes[i]+' student is '+students[i]);
}
</script>
</body>
</html>