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;
--></script>

- 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;
--></script>

- 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.
Example:
<script type="text/javascript"><!--
var sites = ['coursesweb.net', 'www.marplo.net', 'www.google.com'];
var a_site = sites[1];
var other_site = sites[2];
alert(a_site);              // www.marplo.net
--></script>
- Thus, the variable "a_site" will have the value "www.marplo.net" and the variable "other_site" gets the value "www.google.com".

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.
Syntax:

array_name[index] = 'new value';
Example:
<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
--></script>

• 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
--></script>

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 />');
} 
--></script>
- It will display the following result.
php
html
css
flash

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
What attribute makes an option from <select> selected?
checked="checked" selected="selected" disabled="disabled"
<select name="a_name">
 <option value="val1">Option 1</option>
 <option value="val2" selected="selected">Option 2</option>
</select>
What CSS value allows to create color gradients for background?
contain repeat-x linear-gradient
#id {
  background: linear-gradient(top left, #1f1, #fff, #11f);
}
What statement creates an array in JavaScript?
[] {} new Object()
var arr = [1, "CoursesWeb.net", "MarPlo.net"];
alert(arr[2]);
Indicate the PHP function used to redirect to other page.
function() header() switch()
header("Location: http://coursesweb.net/");
exit;
JavaScript Arrays

Last accessed pages

  1. Forms and Input (415)
  2. Add Text in Canvas from Input text field, as it is Typed (266)
  3. PHP MySQL - SELECT, ORDER BY (600)
  4. Add and Delete options in Select list using JavaScript (257)
  5. jQuery ajax() method (1236)

Top accessed pages

  1. PHP-MySQL free course, online tutorials PHP MySQL code (3490)
  2. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (3355)
  3. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (3300)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (3082)
  5. PHP Chat Script (2761)