www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons JavaScript Arrays Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
Lesson 45
Lesson 46
Lesson 47
Lesson 48
Lesson 49
Array is a special data structure in JavaScript that helps us store data conveniently.

Suppose that in our JavaScript program we need to store information about daily temparatures for the whole week, or month, or even a year! If we use variables to store such data we would end up with a piece of code that is untidy, messy and very hard to maintain and read.

For such situations JavaScript provides us with a neat solution called an array. An array is a very efficient and elegant means of storing such data. We use arrays for holding a set of data or values in a single variable name.

The syntax for defining an array is very simple:

var ourArray=new Array();

Notice that creating an array is a little different than creating a simple variable. This is because there are a number of properties associated with arrays. For instance every array has a 'length' property that tells us how many element the array contains.

Code example follows:

<script typbre="text/JavaScript">
var daysOfWeek = new Array();
daysOfWeek[0] = "Friday";
daysOfWeek[1] = "Saturday";
daysOfWeek[2] = "Sunday";
daysOfWeek[3] = "Monday";
daysOfWeek[4] = "Tuesday";
daysOfWeek[5] = "Wednesday";
daysOfWeek[6] = "Thursday";

The above code defines an array variable named daysOfWeek and then adds seven elements to it. We may add more if we need.

The above array can also be defined as:

daysOfWeek = ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"];

We access array elements by referring to their index in the square brackets []. Array index starts at number 0. The following example prints the element at index 0 in the above array:

document.write("Today is "+ daysOfWeek[0]);

Modifying the contents of an array is very simple:

daysOfWeek[2] = "Weekend"; // the third element now has the value Weekend
Next >>> Lesson No. 17: JavaScript Array Functions

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.