Java Script Lesson 3 - Arrays
JavaScript Lesson 3 ’ Arrays
Now that you understand how JavaScript objects work, you are ready to move on to arrays. Arrays can help you display information in an ordered format and can be useful for creating lists, and other necessary blocks of information on your website.
Arrays refer to strings of like elements, for example: Blue, Red, Green and Yellow are all colors. The Nina, Pinta and Santa Maria are all names of ships. When you have several elements that fall into the same string, you can use an array to display them.
Let’s start with a sample array:

Now, let’s look at what we’ve done so far. We have named our variable fruit (var fruit). We have also stated how many entries would be in our array: (newArray(4).) We then began our list of fruits, starting with 0 as the number. You must always start your arrays with a zero for the first entry. Since we stated that there would be 4 entries, we then number each entry in the script, ie: [2]grape.
The next part of the script covers what the background color should be for this array, and how it should be displayed.
Now, that was simple, wasn’t it? You can try this on your own using our above example and just replacing the variables with your own content. Just make sure that you number the amount of items that will be in your array correctly or your script will not function correctly. It is a common mistake to forget that you are starting with the number 0 in your area and referring to the amount of entries as 3 instead of 4, using our example above.
Arrays often appear in many different kinds of scripts, such as menus, navigation bars and just plain lists. See how many ways you can implement an array on your page. Feel free to modify the example script we have given you to play around with your existing data and turn it into an array on your page.
Our next lesson will cover JavaScript functions and how they can be used on your site. You can even make your own functions as long as you follow some simple guidelines.
Related Posts:Posted on 12/27/05 1:46 AM
Be the first to comment!