The Array Object is used to store multiple values in a single variable name.
Each stored value becomes an array element, it has assigned an "index number" (or key). With this key you can refer to any element of that Array.
There are various ways to create an array:

- Using the "new" keyword

<script type="text/javascript"><!--
var ar_name1 = new Array('CoursesWeb', 'lessons', 5);
// or
var ar_name2 = new Array();
  ar_name2[0] = 'CoursesWeb';
  ar_name2[1] = 'lessons';
  ar_name2[2] = 5;

- Using square brackets (Literal Array)

<script type="text/javascript"><!--
var ar_name3 = ['tutorials', 'programming', 78];
// or
var ar_name4 = [];
  ar_name4[0] = 'tutorials';
  ar_name4[1] = 'programming';
  ar_name4[2] = 78;

- The index number of items starts at 0.

1. Accessing an Array's elements

To access the elements of an Array, you use an "index number" that allows you access to each element of the array by its position in the array.
The syntax to assign the first element of an array to a variable is the following:

var variable_name = array_name[0];
- array_name is the name of the Array.
- The 0 in brackets is the index number for the first element of the array, the second has an index number of 1, the third has an index number of 2, and so on.
<script type="text/javascript"><!--
var sites = ['', '', ''];
var a_site = sites[1];
var other_site = sites[2];
alert(a_site);              //
- Thus, the variable "a_site" will have the value "" and the variable "other_site" gets the value "".

2. Modify Values in an Array

To modify a value in an array, assign a new value to the element with a specified index number.

array_name[index] = 'new value';
<script type="text/javascript"><!--
var fruits = ['apple', 'pear', 'banana'];
document.write(fruits[2]);                // banana

// change the value
fruits[2] = 'apricot';
document.write('<br />'+ fruits[2]);                // apricot

• Like strings, arrays have one built-in property: length. This property contains a numeric value equal to the number of elements in an array.
<script type="text/javascript"><!--
var fruits = ['apple', 'pear', 'banana'];
alert(fruits.length);              // 3

3. Array Methods

4. Moving Through Arrays

The elements of an Array can easily be parsed with the for() instruction.
The following example uses a "for()" instruction which moves through an array, and displays the value of each item.

<script type="text/javascript"><!--
var tutorials = ["php", "html", "css", "flash"];
var nr_items = tutorials.length;           // gets the number of elements

// gets and output every element of the "tutorials" array
for(i=0; i<nr_items; i++) {
  document.write(tutorials[i] + '<br />');
- It will display the following result.

Daily Test with Code Example

Which tag renders as emphasized text, displaying the text oblique?
<strong> <pre> <em>
<p>Web development courses: <em></em></p>
Which CSS property defines the space between the element border and its content?
margin padding position
h3 {
  padding: 2px 0.2em;
Click on the method which returns the first element that matches a specified group of selectors.
getElementsByName() querySelector() querySelectorAll()
// gets first Div with class="cls", and shows its content
var elm = document.querySelector("div.cls");
Indicate the PHP variable that contains data from a form sent with method="post".
if(isset($_POST["field"])) {
  echo $_POST["field"];
JavaScript Arrays

Last accessed pages

  1. World Striker 2014 (220)
  2. Sport Games (7002)
  3. Drag Racer V3 (14192)
  4. Zuma Deluxe (37764)
  5. Skill Games (6222)

Popular pages this month

  1. Qwop (6303)
  2. Drag Racer V3 (4297)
  3. Bubbles3 (1796)
  4. List with All the Games (1481)
  5. Butterfly Kyodai (1413)