O'Reilly logo

JavaScript™ 1.5 by Example by Kathie Kingsley-Hughes, Adrian Kingsley-Hughes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Arrays

Arrays are a powerful feature of most programming languages, and they are also available to the JavaScripter! An array enables you to store several separate values within a single variable. Usually, all these values have a connecting theme (such as days of the week). Arrays are great because when you know how to use them they can really simplify your code—saving you from having to create a bunch of separate variables with similar names.

So, let's look at how to create and use arrays. The example here will be used to hold the seven days of the week.

The first thing you need to do is to bring the array into existence:

 <html> <head> <title>A Simple Page</title> <script language="JavaScript"> <!-- Cloaking device on! var days_of_week ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required